All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.googlecode.objectify.Ref Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
package com.googlecode.objectify;

import java.io.Serializable;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.NotFoundException;
import com.googlecode.objectify.impl.ref.DeadRef;

/**
 * 

GWT emulation of the Ref class. Not complete; there's a lot we can't do client-side.

* * @author Jeff Schnitzer */ public class Ref implements Serializable, Comparable> { private static final long serialVersionUID = 1L; Key key; T value; /** */ public static Ref create(Key key) { if (key == null) throw new NullPointerException("Cannot create a Ref from a null key"); return new DeadRef(key); } /** */ public static Ref create(Key key, T value) { return new DeadRef(key, value); } /** Doesn't set the key! Dangerous. */ public static Ref create(T value) { return new DeadRef(value); } /** For GWT */ protected Ref() {} /** */ public Ref(Key key) { this.key = key; } /** */ public Ref(T value) { this.value = value; } /** */ public Ref(Key key, T value) { this.key = key; this.value = value; } /** */ public Key key() { if (key == null) throw new IllegalStateException("This ref was created without a key, and we cannot determine keys on GWT client-side"); return key; } /** */ public T get() { return value; } /** */ public T getValue() { return value; } /** */ final public Key getKey() { return key(); } /** */ final public Key safeKey() { Key k = this.key(); if (k == null) throw new NotFoundException(); else return k; } /** */ final public T safeGet() { T t = this.get(); if (t == null) throw new NotFoundException(key()); else return t; } /** Comparison is based on key */ @Override public int compareTo(Ref o) { return this.key().compareTo(o.key()); } /** Equality comparison is based on key equivalence */ @Override public boolean equals(Object obj) { return obj != null && obj instanceof Ref && key().equals(((Ref)obj).key()); } /** Type-safe comparison for key equivalence */ public boolean equivalent(Ref other) { return equals(other); } /** Type safe comparison for key equivalence */ public boolean equivalent(Key other) { return key().equivalent(other); } /** Hash code is simply that of key */ @Override public int hashCode() { return key().hashCode(); } /** Renders some info about the key */ @Override public String toString() { return "Ref(key=" + key() + ", value=" + value + ")"; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy