Revised the hashCode of Id to be based just on the value. Also, revised the equals method to return true when the only difference in the type is that one class is Foo and other is Foo<?, ?>. Added a compact toString() in the Id class that prints the type as MyType<Foo,Bar> instead of fully qualified names for each of the types.

Added tests regarding object equality for the Id class.
This commit is contained in:
Inderjeet Singh 2010-10-23 15:34:57 +00:00
parent d3eda04f33
commit 1d2648231f
2 changed files with 115 additions and 7 deletions

View File

@ -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 <R> type variable for the rest resource
*/
public final class Id<R> {
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<R> {
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<R> {
Id<R> other = (Id<R>)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 <RS> Id<RS> get(long value, Type typeOfId) {
return new Id<RS>(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();
}
}

View File

@ -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<Id<Foo>>(){}.getType();
assertFalse(Id.areEquivalentTypes(type, Id.class));
}
public void testRawTypeEqualToParameterizedOfWildcardType() {
ParameterizedType fooType = (ParameterizedType) new TypeToken<Id<?>>(){}.getType();
assertTrue(Id.areEquivalentTypes(fooType, Id.class));
}
private static class Foo {
}
}