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; ...@@ -6,8 +6,9 @@ package com.google.common.io.protocol;
import java.util.*; 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 { public class ProtoBufType {
// Note: Values 0..15 are reserved for wire types! // Note: Values 0..15 are reserved for wire types!
...@@ -41,46 +42,11 @@ public class ProtoBufType { ...@@ -41,46 +42,11 @@ public class ProtoBufType {
public static final int REQUIRED = 0x100; public static final int REQUIRED = 0x100;
public static final int OPTIONAL = 0x200; public static final int OPTIONAL = 0x200;
public static final int REPEATED = 0x400; public static final int REPEATED = 0x400;
private final IntMap types = new IntMap(); private final StringBuffer types = new StringBuffer();
private final Vector data = new Vector();
/*
* 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 String typeName; private final String typeName;
/** /**
* Empty constructor. * Empty constructor.
*/ */
...@@ -108,36 +74,35 @@ public class ProtoBufType { ...@@ -108,36 +74,35 @@ public class ProtoBufType {
* @return this is returned to permit cascading * @return this is returned to permit cascading
*/ */
public ProtoBufType addElement(int optionsAndType, int tag, Object data) { public ProtoBufType addElement(int optionsAndType, int tag, Object data) {
types.put(tag, new TypeInfo(optionsAndType, data)); while (types.length() <= tag) {
return this; types.append((char) TYPE_UNDEFINED);
} this.data.addElement(null);
}
types.setCharAt(tag, (char) optionsAndType);
this.data.setElementAt(data, tag);
/** return this;
* 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();
} }
/** /**
* Returns the type for the given tag id (without modifiers such as OPTIONAL, * Returns the type for the given tag id (without modifiers such as OPTIONAL,
* REPEATED). For undefined tags, TYPE_UNDEFINED is returned. * REPEATED). For undefined tags, TYPE_UNDEFINED is returned.
*/ */
public int getType(int tag) { public int getType(int tag) {
TypeInfo typeInfo = (TypeInfo) types.get(tag); return (tag < 0 || tag >= types.length())
return typeInfo == null ? TYPE_UNDEFINED : typeInfo.type & MASK_TYPE; ? TYPE_UNDEFINED
: (types.charAt(tag) & MASK_TYPE);
} }
/** /**
* Returns a bit combination of the modifiers for the given tag id * Returns a bit combination of the modifiers for the given tag id
* (OPTIONAL, REPEATED, REQUIRED). For undefined tags, OPTIONAL|REPEATED * (OPTIONAL, REPEATED, REQUIRED). For undefined tags, OPTIONAL|REPEATED
* is returned. * is returned.
*/ */
public int getModifiers(int tag) { public int getModifiers(int tag) {
TypeInfo typeInfo = (TypeInfo) types.get(tag); return (tag < 0 || tag >= types.length())
return typeInfo == null ? (OPTIONAL | REPEATED) : typeInfo.type & MASK_MODIFIER; ? (OPTIONAL | REPEATED)
: (types.charAt(tag) & MASK_MODIFIER);
} }
/** /**
...@@ -146,15 +111,14 @@ public class ProtoBufType { ...@@ -146,15 +111,14 @@ public class ProtoBufType {
* tags, null is returned. * tags, null is returned.
*/ */
public Object getData(int tag) { public Object getData(int tag) {
TypeInfo typeInfo = (TypeInfo) types.get(tag); return (tag < 0 || tag >= data.size()) ? null : data.elementAt(tag);
return typeInfo == null ? typeInfo : typeInfo.data;
} }
/** /**
* Returns the type name set in the constructor for debugging purposes. * Returns the type name set in the constructor for debugging purposes.
*/ */
public String toString() { public String toString() {
return "ProtoBufType Name: " + typeName; return typeName;
} }
/** /**
...@@ -174,9 +138,9 @@ public class ProtoBufType { ...@@ -174,9 +138,9 @@ public class ProtoBufType {
} }
ProtoBufType other = (ProtoBufType) object; ProtoBufType other = (ProtoBufType) object;
return types.equals(other.types); return stringEquals(types, other.types);
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
...@@ -187,4 +151,20 @@ public class ProtoBufType { ...@@ -187,4 +151,20 @@ public class ProtoBufType {
return super.hashCode(); 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 { ...@@ -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 "". */ /** Convenience method to return a string value from of a sub-proto or "". */
public static String getSubProtoValueOrEmpty( public static String getSubProtoValueOrEmpty(
ProtoBuf proto, int sub, int tag) { 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