~ubuntu-branches/ubuntu/quantal/libpal-java/quantal

« back to all changes in this revision

Viewing changes to src/pal/misc/LabelMapping.java

  • Committer: Package Import Robot
  • Author(s): Andreas Tille
  • Date: 2012-01-27 13:57:54 UTC
  • Revision ID: package-import@ubuntu.com-20120127135754-d63l3581f65fw9pk
Tags: upstream-1.5.1
ImportĀ upstreamĀ versionĀ 1.5.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// LableMapping.java
 
2
//
 
3
// (c) 1999-2001 PAL Development Core Team
 
4
//
 
5
// This package may be distributed under the
 
6
// terms of the Lesser GNU General Public License (LGPL)
 
7
 
 
8
package pal.misc;
 
9
 
 
10
/**
 
11
 * Title:        LabelMapping
 
12
 * Description:  Allows for the substitution of one label for another
 
13
 * @author                       Matthew Goode
 
14
 * @version 1.0
 
15
 */
 
16
import java.util.*;
 
17
public class LabelMapping implements java.io.Serializable {
 
18
        Hashtable mappings_ = new Hashtable();
 
19
 
 
20
        //
 
21
        // Serialization code
 
22
        //
 
23
        private static final long serialVersionUID=-9217142171228146380L;
 
24
 
 
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_);
 
29
        }
 
30
 
 
31
        private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException{
 
32
                byte version = in.readByte();
 
33
                switch(version) {
 
34
                        default : {
 
35
                                mappings_ = (Hashtable)in.readObject();
 
36
                                break;
 
37
                        }
 
38
                }
 
39
        }
 
40
        private LabelMapping(Hashtable mapping) {
 
41
                this.mappings_ = mapping;
 
42
        }
 
43
        private LabelMapping(LabelMapping toCopy) {
 
44
                this.mappings_ = (Hashtable)toCopy.mappings_.clone();
 
45
        }
 
46
        public LabelMapping() { }
 
47
 
 
48
        public void addMapping(String id, String label) {
 
49
                mappings_.put(id,label);
 
50
        }
 
51
        public void addMapping(Identifier id, String label) {
 
52
                if(id!=null&&id.getName()!=null) {
 
53
                        mappings_.put(id.getName(),label);
 
54
                }
 
55
        }
 
56
        /**
 
57
         * @param names Names
 
58
         * @param colours associated colours
 
59
         * @note assumes parallel arrays
 
60
         */
 
61
        public void addMappings(String[] ids, String[] labels) {
 
62
                for(int i = 0 ; i < ids.length ; i++) {
 
63
                        mappings_.put(ids[i],labels[i]);
 
64
                }
 
65
        }
 
66
 
 
67
        public String getLabel(String id, String defaultLabel) {
 
68
                if(id==null||!mappings_.containsKey(id)) {
 
69
                        return defaultLabel;
 
70
                }
 
71
                return mappings_.get(id).toString();
 
72
        }
 
73
        public String getLabel(Identifier id, String defaultLabel) {
 
74
                if(id==null) {
 
75
                        return defaultLabel;
 
76
                }
 
77
                return getLabel(id.getName(),defaultLabel);
 
78
        }
 
79
        public String getLabel(Identifier id) {
 
80
                return getLabel(id.getName(),id.getName());
 
81
        }
 
82
        public Identifier getLabelIdentifier(Identifier id) {
 
83
                if(id==null) {
 
84
                        return null;
 
85
                }
 
86
                return new Identifier(getLabel(id.getName(),id.getName()));
 
87
        }
 
88
        /**
 
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
 
90
         */
 
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);
 
96
                        int count = 1;
 
97
                        if(totals.containsKey(mapping)) {
 
98
                                count = ((Integer)totals.get(mapping)).intValue()+1;
 
99
                        }
 
100
                        totals.put(mapping,new Integer(count));
 
101
                }
 
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();
 
108
                        if(total==1) {
 
109
                                result.put(key,mapping);
 
110
                        } else {
 
111
                                int count = 1;
 
112
                                if(counts.containsKey(mapping)) {
 
113
                                        count = ((Integer)counts.get(mapping)).intValue()+1;
 
114
                                }
 
115
                                counts.put(mapping,new Integer(count));
 
116
                                result.put(key,mapping+" "+count);
 
117
                        }
 
118
                }
 
119
                return new LabelMapping(result);
 
120
        }
 
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));
 
127
                }
 
128
                return new LabelMapping(newMapping);
 
129
        }
 
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]);
 
135
                }
 
136
                return new SimpleIdGroup(newIDs);
 
137
        }
 
138
 
 
139
        // Static classes
 
140
 
 
141
        public static interface Relabeller {
 
142
                public String getNewLabel(String oldLabel);
 
143
        }
 
144
}
 
 
b'\\ No newline at end of file'