juzu.impl.metamodel.Key Maven / Gradle / Ivy
/*
* Copyright 2013 eXo Platform SAS
*
* 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 juzu.impl.metamodel;
import juzu.impl.common.JSON;
import java.io.Serializable;
/** @author Julien Viet */
public abstract class Key implements Serializable {
public static Key of(Object value, Class type) {
return new Wrapper(value, type);
}
public static Key of(Class type) {
return new Literal(type);
}
public abstract JSON toJSON();
protected abstract Class getType();
@Override
public abstract boolean equals(Object obj);
@Override
public abstract int hashCode();
private static final class Literal extends Key {
/** . */
private final Class type;
private Literal(Class type) throws NullPointerException {
if (type == null) {
throw new NullPointerException("No null type accepted");
}
//
this.type = type;
}
@Override
public JSON toJSON() {
return new JSON().set("type", type.getName());
}
@Override
protected Class getType() {
return type;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof Literal>) {
Literal> that = (Literal>)obj;
return type.equals(that.type);
}
return false;
}
@Override
public int hashCode() {
return type.hashCode();
}
@Override
public String toString() {
return getClass().getSimpleName() + "[" + type.getName() + "]";
}
}
private static final class Wrapper extends Key {
/** . */
private final Object value;
/** . */
private final Class type;
private Wrapper(Object value, Class type) throws NullPointerException {
if (value == null) {
throw new NullPointerException("No null value accepted");
}
if (type == null) {
throw new NullPointerException("No null type accepted");
}
//
this.value = value;
this.type = type;
}
@Override
protected Class getType() {
return type;
}
@Override
public JSON toJSON() {
return new JSON().set("value", value).set("type", type.getName());
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof Wrapper>) {
Wrapper> that = (Wrapper>)obj;
return value.equals(that.value) && type.equals(that.type);
}
return false;
}
@Override
public int hashCode() {
return value.hashCode() ^ type.hashCode();
}
@Override
public String toString() {
return getClass().getSimpleName() + "[value=" + value + ",type=" + type.getName() + "]";
}
}
}