package com.oreilly.hh;
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 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 transient String description;
/**
* 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;
}
/**
* Keeps track of all instances by name, for efficient lookup.
*/
private static final Map instancesByName = 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.
*/
private SourceMedia(String name, String description) {
this.name = name;
this.description = description;
// Record this instance in the collection that tracks the enumeration
instancesByName.put(name, this);
}
/**
* The instance that represents music obtained from cassette tape.
*/
public static final SourceMedia CASSETTE =
new SourceMedia("cassette", "Audio Cassette Tape");
/**
* The instance that represents music obtained from vinyl.
*/
public static final SourceMedia VINYL =
new SourceMedia("vinyl", "Vinyl Record");
/**
* The instance that represents music obtained from VHS tapes.
*/
public static final SourceMedia VHS =
new SourceMedia("vhs", "VHS Videocassette Tape");
/**
* The instance that represents music obtained from a compact disc.
*/
public static final SourceMedia CD =
new SourceMedia("cd", "Compact Disc");
/**
* The instance that represents music obtained from a broadcast.
*/
public static final SourceMedia BROADCAST =
new SourceMedia("broadcast", "Analog Broadcast");
/**
* The instance that represents music obtained as an Internet download.
*/
public static final SourceMedia DOWNLOAD =
new SourceMedia("download", "Internet Download");
/**
* The instance that represents music from a digital audio stream.
*/
public static final SourceMedia STREAM =
new SourceMedia("stream", "Digital Audio Stream");
/**
* 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;
}
/**
* 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);
}
}
|