package com.oreilly.hh;
import net.sf.hibernate.PersistentEnum;
import java.util.*;
import java.io.Serializable;
/**
* This is a typesafe enumeration that identifies the media on which an
* item in our music database was obtained.
**/
public class SourceMedia implements PersistentEnum, Serializable {
/**
* Stores the external name of this instance, by which it can be retrieved.
*/
private final String name;
/**
* Stores the human-readable description of this instance, by which it is
* identified in the user interface.
*/
private final String description;
/**
* Stores the integer value used by Hibernate to persist this instance.
*/
private final int code;
/**
* Return the external name associated with this instance. <p>
*
* @return the name by which this instance is identified in code.
**/
public String getName() {
return name;
}
/**
* Return the description associated with this instance. <p>
*
* @return the human-readable description by which this instance is
* identified in the user interface.
**/
public String getDescription() {
return description;
}
/**
* Return the persistence code associated with this instance, as
* mandated by the {@link PersistentEnum} interface.
*/
public int toInt() {
return code;
}
/**
* Keeps track of all instances by name, for efficient lookup.
*/
private static final Map instancesByName = new HashMap();
/**
* Keeps track of all instances by code, for efficient lookup.
*/
private static final Map instancesByCode = new HashMap();
/**
* Constructor is private to prevent instantiation except during class
* loading.
*
* @param name the external name of the message type.
* @param description the human readable description of the message type,
* by which it is presented in the user interface.
* @param code the persistence code by which Hibernate stores the instance.
*/
private SourceMedia(String name, String description, int code) {
this.name = name;
this.description = description;
this.code = code;
// Record this instance in the collections that track the enumeration
instancesByName.put(name, this);
instancesByCode.put(new Integer(code), this);
}
/**
* The instance that represents music obtained from cassette tape.
*/
public static final SourceMedia CASSETTE =
new SourceMedia("cassette", "Audio Cassette Tape", 0);
/**
* The instance that represents music obtained from vinyl.
*/
public static final SourceMedia VINYL =
new SourceMedia("vinyl", "Vinyl Record", 1);
/**
* The instance that represents music obtained from VHS tapes.
*/
public static final SourceMedia VHS =
new SourceMedia("vhs", "VHS Videocassette Tape", 2);
/**
* The instance that represents music obtained from a broadcast.
*/
public static final SourceMedia BROADCAST =
new SourceMedia("broadcast", "Analog Broadcast", 3);
/**
* The instance that represents music obtained from a compact disc.
*/
public static final SourceMedia CD =
new SourceMedia("cd", "Compact Disc", 4);
/**
* The instance that represents music obtained as an Internet download.
*/
public static final SourceMedia DOWNLOAD =
new SourceMedia("download", "Internet Download", 5);
/**
* The instance that represents music from a digital audio stream.
*/
public static final SourceMedia STREAM =
new SourceMedia("stream", "Digital Audio Stream", 6);
/**
* Obtain the collection of all legal enumeration values.
*
* @return all instances of this typesafe enumeration.
*/
public static Collection getAllValues() {
return Collections.unmodifiableCollection(instancesByName.values());
}
/**
* Look up an instance by name.
*
* @param name the external name of an instance.
* @return the corresponding instance.
* @throws NoSuchElementException if there is no such instance.
*/
public static SourceMedia getInstanceByName(String name) {
SourceMedia result = (SourceMedia)instancesByName.get(name);
if (result == null) {
throw new NoSuchElementException(name);
}
return result;
}
/**
* Look up an instance by code, as specified by the {@link PersistentEnum}
* interface.
*
* @param code the persistence code of an instance.
* @return the corresponding instance.
* @throws NoSuchElementException if there is no such instance.
*/
public static SourceMedia fromInt(int code) {
SourceMedia result =
(SourceMedia)instancesByCode.get(new Integer(code));
if (result == null) {
throw new NoSuchElementException("code=" + code);
}
return result;
}
/**
* Return a string representation of this object.
*/
public String toString() {
return description;
}
/**
* Insure that deserialization preserves the signleton property.
*/
private Object readResolve() {
return getInstanceByName(name);
}
}
|