Skip to content

Commit

Permalink
Format the code following Google Java Style Guide
Browse files Browse the repository at this point in the history
  • Loading branch information
MaicolAntali committed Dec 16, 2022
1 parent cea692d commit f5e2e16
Show file tree
Hide file tree
Showing 108 changed files with 2,087 additions and 2,223 deletions.
Expand Up @@ -41,6 +41,34 @@ public Cart(List<LineItem> lineItems, String buyerName, String creditCard) {
this.creditCard = creditCard;
}

@SuppressWarnings("unchecked")
public static String getSimpleTypeName(Type type) {
if (type == null) {
return "null";
}
if (type instanceof Class) {
return ((Class)type).getSimpleName();
} else if (type instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) type;
StringBuilder sb = new StringBuilder(getSimpleTypeName(pType.getRawType()));
sb.append('<');
boolean first = true;
for (Type argumentType : pType.getActualTypeArguments()) {
if (first) {
first = false;
} else {
sb.append(',');
}
sb.append(getSimpleTypeName(argumentType));
}
sb.append('>');
return sb.toString();
} else if (type instanceof WildcardType) {
return "?";
}
return type.toString();
}

public List<LineItem> getLineItems() {
return lineItems;
}
Expand Down Expand Up @@ -77,32 +105,4 @@ public String toString() {
+ "LINE_ITEMS: " + itemsText.toString() + "]";
}

@SuppressWarnings("unchecked")
public static String getSimpleTypeName(Type type) {
if (type == null) {
return "null";
}
if (type instanceof Class) {
return ((Class)type).getSimpleName();
} else if (type instanceof ParameterizedType) {
ParameterizedType pType = (ParameterizedType) type;
StringBuilder sb = new StringBuilder(getSimpleTypeName(pType.getRawType()));
sb.append('<');
boolean first = true;
for (Type argumentType : pType.getActualTypeArguments()) {
if (first) {
first = false;
} else {
sb.append(',');
}
sb.append(getSimpleTypeName(argumentType));
}
sb.append('>');
return sb.toString();
} else if (type instanceof WildcardType) {
return "?";
}
return type.toString();
}

}
Expand Up @@ -15,27 +15,13 @@
*/
package com.google.gson.extras.examples.rawcollections;

import java.util.ArrayList;
import java.util.Collection;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import java.util.ArrayList;
import java.util.Collection;

public class RawCollectionsExample {
static class Event {
private String name;
private String source;
private Event(String name, String source) {
this.name = name;
this.source = source;
}
@Override
public String toString() {
return String.format("(name=%s, source=%s)", name, source);
}
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public static void main(String[] args) {
Gson gson = new Gson();
Expand All @@ -51,4 +37,17 @@ public static void main(String[] args) {
Event event = gson.fromJson(array.get(2), Event.class);
System.out.printf("Using Gson.fromJson() to get: %s, %d, %s", message, number, event);
}

static class Event {
private String name;
private String source;
private Event(String name, String source) {
this.name = name;
this.source = source;
}
@Override
public String toString() {
return String.format("(name=%s, source=%s)", name, source);
}
}
}
Expand Up @@ -269,23 +269,20 @@ static class Element<T> {
* This element's name in the top level graph object.
*/
private final String id;

/**
* The element to deserialize. Unused in serialization.
*/
private final JsonElement element;
/**
* The value if known. During deserialization this is lazily populated.
*/
private T value;

/**
* This element's type adapter if known. During deserialization this is
* lazily populated.
*/
private TypeAdapter<T> typeAdapter;

/**
* The element to deserialize. Unused in serialization.
*/
private final JsonElement element;

Element(T value, String id, TypeAdapter<T> typeAdapter, JsonElement element) {
this.value = value;
this.id = id;
Expand Down
Expand Up @@ -16,18 +16,16 @@

package com.google.gson.typeadapters;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.annotation.PostConstruct;

import com.google.gson.Gson;
import com.google.gson.TypeAdapter;
import com.google.gson.TypeAdapterFactory;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.annotation.PostConstruct;

public class PostConstructAdapterFactory implements TypeAdapterFactory {
// copied from https://gist.github.com/swankjesse/20df26adaf639ed7fd160f145a0b661a
Expand Down
Expand Up @@ -16,6 +16,10 @@

package com.google.gson.typeadapters;

import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.ParsePosition;
Expand All @@ -24,46 +28,12 @@
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;
import com.google.gson.JsonParseException;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

public final class UtcDateTypeAdapter extends TypeAdapter<Date> {
private final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone("UTC");

@Override
public void write(JsonWriter out, Date date) throws IOException {
if (date == null) {
out.nullValue();
} else {
String value = format(date, true, UTC_TIME_ZONE);
out.value(value);
}
}

@Override
public Date read(JsonReader in) throws IOException {
try {
switch (in.peek()) {
case NULL:
in.nextNull();
return null;
default:
String date = in.nextString();
// Instead of using iso8601Format.parse(value), we use Jackson's date parsing
// This is because Android doesn't support XXX because it is JDK 1.6
return parse(date, new ParsePosition(0));
}
} catch (ParseException e) {
throw new JsonParseException(e);
}
}

// Date parsing code from Jackson databind ISO8601Utils.java
// https://github.com/FasterXML/jackson-databind/blob/master/src/main/java/com/fasterxml/jackson/databind/util/ISO8601Utils.java
private static final String GMT_ID = "GMT";
private final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone("UTC");

/**
* Format date into yyyy-MM-ddThh:mm:ss[.sss][Z|[+-]hh:mm]
Expand Down Expand Up @@ -113,6 +83,7 @@ private static String format(Date date, boolean millis, TimeZone tz) {

return formatted.toString();
}

/**
* Zero pad a number to a specified length
*
Expand Down Expand Up @@ -279,4 +250,32 @@ private static int parseInt(String value, int beginIndex, int endIndex) throws N
}
return -result;
}

@Override
public void write(JsonWriter out, Date date) throws IOException {
if (date == null) {
out.nullValue();
} else {
String value = format(date, true, UTC_TIME_ZONE);
out.value(value);
}
}

@Override
public Date read(JsonReader in) throws IOException {
try {
switch (in.peek()) {
case NULL:
in.nextNull();
return null;
default:
String date = in.nextString();
// Instead of using iso8601Format.parse(value), we use Jackson's date parsing
// This is because Android doesn't support XXX because it is JDK 1.6
return parse(date, new ParsePosition(0));
}
} catch (ParseException e) {
throw new JsonParseException(e);
}
}
}
Expand Up @@ -19,17 +19,15 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.junit.Test;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

public final class GraphAdapterBuilderTest {
@Test
public void testSerialization() {
Expand Down

0 comments on commit f5e2e16

Please sign in to comment.