3
// (c) 1999-2001 PAL Development Core Team
5
// This package may be distributed under the
6
// terms of the Lesser GNU General Public License (LGPL)
12
* Description: Allows for the substitution of one label for another
13
* @author Matthew Goode
17
public class LabelMapping implements java.io.Serializable {
18
Hashtable mappings_ = new Hashtable();
23
private static final long serialVersionUID=-9217142171228146380L;
25
//serialver -classpath ./classes pal.misc.LabelMapping
26
private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException {
27
out.writeByte(1); //Version number
28
out.writeObject(mappings_);
31
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException{
32
byte version = in.readByte();
35
mappings_ = (Hashtable)in.readObject();
40
private LabelMapping(Hashtable mapping) {
41
this.mappings_ = mapping;
43
private LabelMapping(LabelMapping toCopy) {
44
this.mappings_ = (Hashtable)toCopy.mappings_.clone();
46
public LabelMapping() { }
48
public void addMapping(String id, String label) {
49
mappings_.put(id,label);
51
public void addMapping(Identifier id, String label) {
52
if(id!=null&&id.getName()!=null) {
53
mappings_.put(id.getName(),label);
58
* @param colours associated colours
59
* @note assumes parallel arrays
61
public void addMappings(String[] ids, String[] labels) {
62
for(int i = 0 ; i < ids.length ; i++) {
63
mappings_.put(ids[i],labels[i]);
67
public String getLabel(String id, String defaultLabel) {
68
if(id==null||!mappings_.containsKey(id)) {
71
return mappings_.get(id).toString();
73
public String getLabel(Identifier id, String defaultLabel) {
77
return getLabel(id.getName(),defaultLabel);
79
public String getLabel(Identifier id) {
80
return getLabel(id.getName(),id.getName());
82
public Identifier getLabelIdentifier(Identifier id) {
86
return new Identifier(getLabel(id.getName(),id.getName()));
89
* If a mapping occurs more than once will rename instance to "x 1", "x 2"... and so on where x is the mapping in question
91
public LabelMapping getUniquifiedMappings() {
92
Hashtable totals = new Hashtable();
93
for(Enumeration e = mappings_.keys() ; e.hasMoreElements() ; ) {
94
Object key = e.nextElement();
95
Object mapping = mappings_.get(key);
97
if(totals.containsKey(mapping)) {
98
count = ((Integer)totals.get(mapping)).intValue()+1;
100
totals.put(mapping,new Integer(count));
102
Hashtable counts = new Hashtable();
103
Hashtable result = new Hashtable();
104
for(Enumeration e = mappings_.keys() ; e.hasMoreElements() ; ) {
105
Object key = e.nextElement();
106
Object mapping = mappings_.get(key);
107
int total = ((Integer)totals.get(mapping)).intValue();
109
result.put(key,mapping);
112
if(counts.containsKey(mapping)) {
113
count = ((Integer)counts.get(mapping)).intValue()+1;
115
counts.put(mapping,new Integer(count));
116
result.put(key,mapping+" "+count);
119
return new LabelMapping(result);
121
public LabelMapping getRelabeled(Relabeller relabeller) {
122
Hashtable newMapping = new Hashtable();
123
for(Enumeration e = mappings_.keys() ; e.hasMoreElements() ; ) {
124
Object key = e.nextElement();
125
String old = mappings_.get(key).toString();
126
newMapping.put(key,relabeller.getNewLabel(old));
128
return new LabelMapping(newMapping);
130
public IdGroup getMapped(IdGroup original) {
131
String[] oldIDs = Identifier.getNames(original);
132
String[] newIDs = new String[oldIDs.length];
133
for(int i = 0 ; i < newIDs.length ; i++) {
134
newIDs[i] = getLabel(oldIDs[i],oldIDs[i]);
136
return new SimpleIdGroup(newIDs);
141
public static interface Relabeller {
142
public String getNewLabel(String oldLabel);
b'\\ No newline at end of file'