Eliminating code overhead
* calculating size of the list once in case of loops, avoided creation of string builder object if the length type argument is 0 * replaced null check boilerplate code with nullSafe()
This commit is contained in:
parent
e0f0854ac2
commit
9e44d60b83
@ -122,7 +122,7 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
|
||||
*/
|
||||
static String separateCamelCase(String name, String separator) {
|
||||
StringBuilder translation = new StringBuilder();
|
||||
for (int i = 0; i < name.length(); i++) {
|
||||
for (int i = 0, length = name.length(); i < length; i++) {
|
||||
char character = name.charAt(i);
|
||||
if (Character.isUpperCase(character) && translation.length() != 0) {
|
||||
translation.append(separator);
|
||||
@ -139,8 +139,9 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
|
||||
StringBuilder fieldNameBuilder = new StringBuilder();
|
||||
int index = 0;
|
||||
char firstCharacter = name.charAt(index);
|
||||
int length = name.length();
|
||||
|
||||
while (index < name.length() - 1) {
|
||||
while (index < length - 1) {
|
||||
if (Character.isLetter(firstCharacter)) {
|
||||
break;
|
||||
}
|
||||
@ -149,7 +150,7 @@ public enum FieldNamingPolicy implements FieldNamingStrategy {
|
||||
firstCharacter = name.charAt(++index);
|
||||
}
|
||||
|
||||
if (index == name.length()) {
|
||||
if (index == length) {
|
||||
return fieldNameBuilder.toString();
|
||||
}
|
||||
|
||||
|
@ -411,7 +411,7 @@ public final class $Gson$Types {
|
||||
}
|
||||
|
||||
private static int indexOf(Object[] array, Object toFind) {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
for (int i = 0, length = array.length; i < length; i++) {
|
||||
if (toFind.equals(array[i])) {
|
||||
return i;
|
||||
}
|
||||
@ -451,7 +451,7 @@ public final class $Gson$Types {
|
||||
this.ownerType = ownerType == null ? null : canonicalize(ownerType);
|
||||
this.rawType = canonicalize(rawType);
|
||||
this.typeArguments = typeArguments.clone();
|
||||
for (int t = 0; t < this.typeArguments.length; t++) {
|
||||
for (int t = 0, length = this.typeArguments.length; t < length; t++) {
|
||||
checkNotNull(this.typeArguments[t]);
|
||||
checkNotPrimitive(this.typeArguments[t]);
|
||||
this.typeArguments[t] = canonicalize(this.typeArguments[t]);
|
||||
@ -482,15 +482,14 @@ public final class $Gson$Types {
|
||||
}
|
||||
|
||||
@Override public String toString() {
|
||||
StringBuilder stringBuilder = new StringBuilder(30 * (typeArguments.length + 1));
|
||||
stringBuilder.append(typeToString(rawType));
|
||||
|
||||
if (typeArguments.length == 0) {
|
||||
return stringBuilder.toString();
|
||||
int length = typeArguments.length;
|
||||
if (length == 0) {
|
||||
return typeToString(rawType);
|
||||
}
|
||||
|
||||
stringBuilder.append("<").append(typeToString(typeArguments[0]));
|
||||
for (int i = 1; i < typeArguments.length; i++) {
|
||||
StringBuilder stringBuilder = new StringBuilder(30 * (length + 1));
|
||||
stringBuilder.append(typeToString(rawType)).append("<").append(typeToString(typeArguments[0]));
|
||||
for (int i = 1; i < length; i++) {
|
||||
stringBuilder.append(", ").append(typeToString(typeArguments[i]));
|
||||
}
|
||||
return stringBuilder.append(">").toString();
|
||||
|
@ -73,8 +73,10 @@ public final class ArrayTypeAdapter<E> extends TypeAdapter<Object> {
|
||||
list.add(instance);
|
||||
}
|
||||
in.endArray();
|
||||
Object array = Array.newInstance(componentType, list.size());
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
|
||||
int size = list.size();
|
||||
Object array = Array.newInstance(componentType, size);
|
||||
for (int i = 0; i < size; i++) {
|
||||
Array.set(array, i, list.get(i));
|
||||
}
|
||||
return array;
|
||||
|
@ -158,7 +158,7 @@ public final class ReflectiveTypeAdapterFactory implements TypeAdapterFactory {
|
||||
Type fieldType = $Gson$Types.resolve(type.getType(), raw, field.getGenericType());
|
||||
List<String> fieldNames = getFieldNames(field);
|
||||
BoundField previous = null;
|
||||
for (int i = 0; i < fieldNames.size(); ++i) {
|
||||
for (int i = 0, size = fieldNames.size(); i < size; ++i) {
|
||||
String name = fieldNames.get(i);
|
||||
if (i != 0) serialize = false; // only serialize the default name
|
||||
BoundField boundField = createBoundField(context, field, name,
|
||||
|
@ -69,33 +69,20 @@ public final class TypeAdapters {
|
||||
public static final TypeAdapter<Class> CLASS = new TypeAdapter<Class>() {
|
||||
@Override
|
||||
public void write(JsonWriter out, Class value) throws IOException {
|
||||
if (value == null) {
|
||||
out.nullValue();
|
||||
} else {
|
||||
throw new UnsupportedOperationException("Attempted to serialize java.lang.Class: "
|
||||
+ value.getName() + ". Forgot to register a type adapter?");
|
||||
}
|
||||
throw new UnsupportedOperationException("Attempted to serialize java.lang.Class: "
|
||||
+ value.getName() + ". Forgot to register a type adapter?");
|
||||
}
|
||||
@Override
|
||||
public Class read(JsonReader in) throws IOException {
|
||||
if (in.peek() == JsonToken.NULL) {
|
||||
in.nextNull();
|
||||
return null;
|
||||
} else {
|
||||
throw new UnsupportedOperationException(
|
||||
"Attempted to deserialize a java.lang.Class. Forgot to register a type adapter?");
|
||||
}
|
||||
throw new UnsupportedOperationException(
|
||||
"Attempted to deserialize a java.lang.Class. Forgot to register a type adapter?");
|
||||
}
|
||||
};
|
||||
}.nullSafe();
|
||||
|
||||
public static final TypeAdapterFactory CLASS_FACTORY = newFactory(Class.class, CLASS);
|
||||
|
||||
public static final TypeAdapter<BitSet> BIT_SET = new TypeAdapter<BitSet>() {
|
||||
@Override public BitSet read(JsonReader in) throws IOException {
|
||||
if (in.peek() == JsonToken.NULL) {
|
||||
in.nextNull();
|
||||
return null;
|
||||
}
|
||||
|
||||
BitSet bitset = new BitSet();
|
||||
in.beginArray();
|
||||
int i = 0;
|
||||
@ -132,19 +119,14 @@ public final class TypeAdapters {
|
||||
}
|
||||
|
||||
@Override public void write(JsonWriter out, BitSet src) throws IOException {
|
||||
if (src == null) {
|
||||
out.nullValue();
|
||||
return;
|
||||
}
|
||||
|
||||
out.beginArray();
|
||||
for (int i = 0; i < src.length(); i++) {
|
||||
for (int i = 0, length = src.length(); i < length; i++) {
|
||||
int value = (src.get(i)) ? 1 : 0;
|
||||
out.value(value);
|
||||
}
|
||||
out.endArray();
|
||||
}
|
||||
};
|
||||
}.nullSafe();
|
||||
|
||||
public static final TypeAdapterFactory BIT_SET_FACTORY = newFactory(BitSet.class, BIT_SET);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user