| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (

This example Java source code file ( is included in the "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

assignment, course, jsonparseexception, list, override, parameterizedtype, reflection, string, student, student2, suppresswarnings, type, type_course_history, util

The Java example source code

 * Copyright (C) 2011 Google Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.


import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import junit.framework.TestCase;


 * Collection of functional tests for DOM tree based type adapters.
public class TreeTypeAdaptersTest extends TestCase {
  private static final Id<Student> STUDENT1_ID = new Id("5", Student.class);
  private static final Id<Student> STUDENT2_ID = new Id("6", Student.class);
  private static final Student STUDENT1 = new Student(STUDENT1_ID, "first");
  private static final Student STUDENT2 = new Student(STUDENT2_ID, "second");
  private static final Type TYPE_COURSE_HISTORY =
    new TypeToken<Course(){}.getType(); 
  private static final Id<Course COURSE_ID =
      new Id<Course("10", TYPE_COURSE_HISTORY);

  private Gson gson;
  private Course<HistoryCourse> course;

  protected void setUp() {
    gson = new GsonBuilder()
        .registerTypeAdapter(Id.class, new IdTreeTypeAdapter())
    course = new Course<HistoryCourse>(COURSE_ID, 4,
        new Assignment<HistoryCourse>(null, null), createList(STUDENT1, STUDENT2));

  public void testSerializeId() {
    String json = gson.toJson(course, TYPE_COURSE_HISTORY);

  public void testDeserializeId() {
    String json = "{courseId:1,students:[{id:1,name:'first'},{id:6,name:'second'}],"
      + "numAssignments:4,assignment:{}}";
    Course<HistoryCourse> target = gson.fromJson(json, TYPE_COURSE_HISTORY);
    assertEquals("1", target.getStudents().get(0).id.getValue());
    assertEquals("6", target.getStudents().get(1).id.getValue());
    assertEquals("1", target.getId().getValue());

  private static final class Id<R> {
    final String value;
    final Type typeOfId;

    private Id(String value, Type typeOfId) {
      this.value = value;
      this.typeOfId = typeOfId;
    public String getValue() {
      return value;

  private static final class IdTreeTypeAdapter implements JsonSerializer<Id,
      JsonDeserializer<Id {

    public Id<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
      if (!(typeOfT instanceof ParameterizedType)) {
        throw new JsonParseException("Id of unknown type: " + typeOfT);
      ParameterizedType parameterizedType = (ParameterizedType) typeOfT;
      // Since Id takes only one TypeVariable, the actual type corresponding to the first
      // TypeVariable is the Type we are looking for
      Type typeOfId = parameterizedType.getActualTypeArguments()[0];
      return new Id(json.getAsString(), typeOfId);

    public JsonElement serialize(Id<?> src, Type typeOfSrc, JsonSerializationContext context) {
      return new JsonPrimitive(src.getValue());

  private static class Student {
    Id<Student> id;
    String name;

    private Student() {
      this(null, null);
    public Student(Id<Student> id, String name) { = id; = name;

  private static class Course<T> {
    final List<Student> students;
    private final Id<Course courseId;
    private final int numAssignments;
    private final Assignment<T> assignment;

    private Course() {
      this(null, 0, null, new ArrayList<Student>());

    public Course(Id<Course courseId, int numAssignments,
        Assignment<T> assignment, List players) {
      this.courseId = courseId;
      this.numAssignments = numAssignments;
      this.assignment = assignment;
      this.students = players;
    public Id<Course getId() {
      return courseId;
    List<Student> getStudents() {
      return students;

  private static class Assignment<T> {
    private final Id<Assignment id;
    private final T data;

    private Assignment() {
      this(null, null);
    public Assignment(Id<Assignment id, T data) { = id; = data;

  private static class HistoryCourse {
    int numClasses;

  private static <T> List createList(T ...items) {
    return Arrays.asList(items);

Other Java examples (source code examples)

Here is a short list of links related to this Java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.