diff --git a/wsdef/src/main/java/com/google/gson/webservice/definition/rest/Id.java b/wsdef/src/main/java/com/google/gson/webservice/definition/rest/Id.java index 20b3349d..3b1cae01 100644 --- a/wsdef/src/main/java/com/google/gson/webservice/definition/rest/Id.java +++ b/wsdef/src/main/java/com/google/gson/webservice/definition/rest/Id.java @@ -15,7 +15,9 @@ */ package com.google.gson.webservice.definition.rest; +import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import java.lang.reflect.WildcardType; /** * An id for a rest resource @@ -25,10 +27,12 @@ import java.lang.reflect.Type; * @param type variable for the rest resource */ public final class Id { + private static final long NULL_VALUE = -1; private final long value; private final Type typeOfId; private Id(long value, Type typeOfId) { + Preconditions.checkArgument(value != NULL_VALUE); this.value = value; this.typeOfId = typeOfId; } @@ -37,17 +41,16 @@ public final class Id { return value; } + public static long getValue(Id id) { + return id == null ? NULL_VALUE : id.getValue(); + } public Type getTypeOfId() { return typeOfId; } @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((typeOfId == null) ? 0 : typeOfId.hashCode()); - result = prime * result + (int)(value ^ (value >>> 32)); - return result; + return (int) value; } @Override @@ -59,17 +62,79 @@ public final class Id { Id other = (Id)obj; if (typeOfId == null) { if (other.typeOfId != null) return false; - } else if (!typeOfId.equals(other.typeOfId)) return false; + } else if (!equivalentTypes(typeOfId, other.typeOfId)) return false; if (value != other.value) return false; return true; } + /** + * Returns true for equivalentTypes(Class, Class) + * Visible for testing only + */ + @SuppressWarnings("rawtypes") + static boolean equivalentTypes(Type type1, Type type2) { + if (type1 instanceof ParameterizedType && type2 instanceof Class) { + return areEquivalentTypes((ParameterizedType)type1, (Class)type2); + } else if (type2 instanceof ParameterizedType && type1 instanceof Class) { + return areEquivalentTypes((ParameterizedType)type2, (Class)type1); + } + return type1.equals(type2); + } + + /** + * Visible for testing only + */ + @SuppressWarnings("rawtypes") + static boolean areEquivalentTypes(ParameterizedType type, Class clazz) { + Class rawClass = (Class) type.getRawType(); + if (!clazz.equals(rawClass)) { + return false; + } + for (Type typeVariable : type.getActualTypeArguments()) { + if (typeVariable instanceof WildcardType) { + continue; + } + // This is a real parameterized type, not just ? + return false; + } + return true; + } + public static Id get(long value, Type typeOfId) { return new Id(value, typeOfId); } @Override public String toString() { - return String.format("{value:%s,type:%s}", value, typeOfId); + String typeAsString = getSimpleTypeName(typeOfId); + return String.format("{value:%s,type:%s}", value, typeAsString); + } + + @SuppressWarnings("rawtypes") + private 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(); } } diff --git a/wsdef/src/test/java/com/google/gson/webservice/definition/rest/IdTest.java b/wsdef/src/test/java/com/google/gson/webservice/definition/rest/IdTest.java new file mode 100644 index 00000000..4c41a01a --- /dev/null +++ b/wsdef/src/test/java/com/google/gson/webservice/definition/rest/IdTest.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2010 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.gson.webservice.definition.rest; + +import java.lang.reflect.ParameterizedType; + +import junit.framework.TestCase; + +import com.google.gson.reflect.TypeToken; + +/** + * Unit test for {@link Id} + * + * @author inder + */ +public class IdTest extends TestCase { + + public void testRawTypeNotEqualToParameterizedOfConcreteType() { + ParameterizedType type = (ParameterizedType) new TypeToken>(){}.getType(); + assertFalse(Id.areEquivalentTypes(type, Id.class)); + } + + public void testRawTypeEqualToParameterizedOfWildcardType() { + ParameterizedType fooType = (ParameterizedType) new TypeToken>(){}.getType(); + assertTrue(Id.areEquivalentTypes(fooType, Id.class)); + } + + private static class Foo { + } +}