My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.appenginefan.toolkit.persistence;

import com.google.common.base.Preconditions;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Message;

/**
* Persistence that uses googles protocol buffer framework
* for efficient representation and serialization of data.
*
* @param <T>
* the type of protocol message that this
* persistence is for
*/
public class ProtocolBufferPersistence<T extends Message>
extends MarshallingPersistence<T> {

private final T prototype;

/**
* Constructor
*
* @param backend
* a backend that stores serialized protocol
* buffers
* @param prototype
* a prototype instance of the generic type that
* this peristence object is for.
*/
public ProtocolBufferPersistence(
Persistence<byte[]> backend, T prototype) {
super(backend);
Preconditions.checkNotNull(prototype);
this.prototype = prototype;
}

@Override
protected byte[] makeArray(T nonNullValue) {
return nonNullValue.toByteArray();
}

@SuppressWarnings("unchecked")
@Override
protected T makeType(byte[] nonNullValue) {
try {
return (T) prototype.newBuilderForType().mergeFrom(
nonNullValue).build();
} catch (InvalidProtocolBufferException e) {
throw new StoreException(
"ProtocolBuffer deserialization failed", e);
}
}

}

Change log

r2 by schefflerjens on Apr 17, 2009   Diff
Initial revision
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 1433 bytes, 54 lines
Powered by Google Project Hosting