Commit 7e013cac authored by Mitsuru Oshima's avatar Mitsuru Oshima

Revert "ProtoBuf update"

This reverts commit 9aaf507646c866ab131bf2bcd973882ff9f553cf.
parent babfb778
This diff is collapsed.
......@@ -6,8 +6,9 @@ package com.google.common.io.protocol;
import java.util.*;
/**
* This class can be used to create a memory model of a .proto file.
*
* This class can be used to create a memory model of a .proto file. Currently,
* it is assumed that tags ids are not large. This could be improved by storing
* a start offset, relaxing the assumption to a dense number space.
*/
public class ProtoBufType {
// Note: Values 0..15 are reserved for wire types!
......@@ -42,43 +43,8 @@ public class ProtoBufType {
public static final int OPTIONAL = 0x200;
public static final int REPEATED = 0x400;
private final IntMap types = new IntMap();
/*
* A struct to store field type and default object.
* Two TypeInfo objects are equal iff both have the
* euqal type and object.
*/
static class TypeInfo {
private int type;
private Object data;
TypeInfo(int t, Object d) {
type = t;
data = d;
}
public int hashCode() {
return type;
}
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || !(obj instanceof TypeInfo)) {
return false;
}
TypeInfo peerTypeInfo = (TypeInfo) obj;
return type == peerTypeInfo.type &&
(data == peerTypeInfo.data ||
(data != null && data.equals(peerTypeInfo.data)));
}
public String toString() {
return "TypeInfo{type=" + type + ", data=" + data + "}";
}
};
private final StringBuffer types = new StringBuffer();
private final Vector data = new Vector();
private final String typeName;
/**
......@@ -108,17 +74,14 @@ public class ProtoBufType {
* @return this is returned to permit cascading
*/
public ProtoBufType addElement(int optionsAndType, int tag, Object data) {
types.put(tag, new TypeInfo(optionsAndType, data));
return this;
while (types.length() <= tag) {
types.append((char) TYPE_UNDEFINED);
this.data.addElement(null);
}
types.setCharAt(tag, (char) optionsAndType);
this.data.setElementAt(data, tag);
/**
* Returns a IntMap that has the same lower buffer size as types.
* This is for ProtoBuf to create IntMap with pre-allocated
* internal buffer.
*/
/* package protected */ IntMap newIntMapForProtoBuf() {
return types.newIntMapWithSameBufferSize();
return this;
}
/**
......@@ -126,8 +89,9 @@ public class ProtoBufType {
* REPEATED). For undefined tags, TYPE_UNDEFINED is returned.
*/
public int getType(int tag) {
TypeInfo typeInfo = (TypeInfo) types.get(tag);
return typeInfo == null ? TYPE_UNDEFINED : typeInfo.type & MASK_TYPE;
return (tag < 0 || tag >= types.length())
? TYPE_UNDEFINED
: (types.charAt(tag) & MASK_TYPE);
}
/**
......@@ -136,8 +100,9 @@ public class ProtoBufType {
* is returned.
*/
public int getModifiers(int tag) {
TypeInfo typeInfo = (TypeInfo) types.get(tag);
return typeInfo == null ? (OPTIONAL | REPEATED) : typeInfo.type & MASK_MODIFIER;
return (tag < 0 || tag >= types.length())
? (OPTIONAL | REPEATED)
: (types.charAt(tag) & MASK_MODIFIER);
}
/**
......@@ -146,15 +111,14 @@ public class ProtoBufType {
* tags, null is returned.
*/
public Object getData(int tag) {
TypeInfo typeInfo = (TypeInfo) types.get(tag);
return typeInfo == null ? typeInfo : typeInfo.data;
return (tag < 0 || tag >= data.size()) ? null : data.elementAt(tag);
}
/**
* Returns the type name set in the constructor for debugging purposes.
*/
public String toString() {
return "ProtoBufType Name: " + typeName;
return typeName;
}
/**
......@@ -174,7 +138,7 @@ public class ProtoBufType {
}
ProtoBufType other = (ProtoBufType) object;
return types.equals(other.types);
return stringEquals(types, other.types);
}
/**
......@@ -187,4 +151,20 @@ public class ProtoBufType {
return super.hashCode();
}
}
public static boolean stringEquals(CharSequence a, CharSequence b) {
if (a == b) return true;
int length;
if (a != null && b != null && (length = a.length()) == b.length()) {
if (a instanceof String && b instanceof String) {
return a.equals(b);
} else {
for (int i = 0; i < length; i++) {
if (a.charAt(i) != b.charAt(i)) return false;
}
return true;
}
}
return false;
}
}
......@@ -22,25 +22,6 @@ public final class ProtoBufUtil {
}
}
/** Convenience method to return a string value from of a proto or null. */
public static String getProtoValueOrNull(ProtoBuf proto, int tag) {
try {
return (proto != null && proto.has(tag)) ? proto.getString(tag) : null;
} catch (ClassCastException e) {
return null;
}
}
/** Convenience method to return a string value from of a proto or null. */
public static String getProtoValueOrNull(ProtoBuf proto, int tag, int index) {
try {
return (proto != null && proto.has(tag) && proto.getCount(tag) > index) ?
proto.getString(tag, index) : null;
} catch (ClassCastException e) {
return null;
}
}
/** Convenience method to return a string value from of a sub-proto or "". */
public static String getSubProtoValueOrEmpty(
ProtoBuf proto, int sub, int tag) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment