1 | package org.farng.mp3.object; |
2 | |
3 | import org.farng.mp3.TagConstant; |
4 | |
5 | import java.util.HashMap; |
6 | import java.util.Iterator; |
7 | import java.util.TreeSet; |
8 | |
9 | /** |
10 | * ID3v2 and Lyrics3v2 tags have individual fields <code>AbstractMP3Fragment</code>s Then each fragment is broken down |
11 | * in to individual <code>AbstractMP3Object</code>s |
12 | * |
13 | * @author Eric Farng |
14 | * @version $Revision: 1.4 $ |
15 | */ |
16 | public class ObjectStringHashMap extends ObjectStringFixedLength implements ObjectHashMapInterface { |
17 | |
18 | public static final String LANGUAGE = "Language"; |
19 | HashMap idToString = null; |
20 | HashMap stringToId = null; |
21 | boolean hasEmptyValue = false; |
22 | |
23 | /** |
24 | * Creates a new ObjectStringHashMap object. |
25 | */ |
26 | public ObjectStringHashMap(final String identifier, final int size) { |
27 | super(identifier, size); |
28 | if (identifier.equals(ObjectStringHashMap.LANGUAGE)) { |
29 | this.stringToId = TagConstant.languageStringToId; |
30 | this.idToString = TagConstant.languageIdToString; |
31 | } else { |
32 | throw new IllegalArgumentException("Hashmap identifier not defined in this class: " + identifier); |
33 | } |
34 | } |
35 | |
36 | /** |
37 | * Creates a new ObjectStringHashMap object. |
38 | */ |
39 | public ObjectStringHashMap(final ObjectStringHashMap copyObject) { |
40 | super(copyObject); |
41 | this.hasEmptyValue = copyObject.hasEmptyValue; |
42 | this.idToString = copyObject.idToString; |
43 | this.stringToId = copyObject.stringToId; |
44 | } |
45 | |
46 | public HashMap getIdToString() { |
47 | return this.idToString; |
48 | } |
49 | |
50 | public HashMap getStringToId() { |
51 | return this.stringToId; |
52 | } |
53 | |
54 | public void setValue(final Object value) { |
55 | if (value instanceof String) { |
56 | this.value = ((String) value).toLowerCase(); |
57 | } else { |
58 | this.value = value; |
59 | } |
60 | } |
61 | |
62 | public boolean equals(final Object obj) { |
63 | if ((obj instanceof ObjectStringHashMap) == false) { |
64 | return false; |
65 | } |
66 | final ObjectStringHashMap objectStringHashMap = (ObjectStringHashMap) obj; |
67 | if (this.hasEmptyValue != objectStringHashMap.hasEmptyValue) { |
68 | return false; |
69 | } |
70 | if (this.idToString == null) { |
71 | if (objectStringHashMap.idToString != null) { |
72 | return false; |
73 | } |
74 | } else { |
75 | if (this.idToString.equals(objectStringHashMap.idToString) == false) { |
76 | return false; |
77 | } |
78 | } |
79 | if (this.idToString == null) { |
80 | if (objectStringHashMap.idToString != null) { |
81 | return false; |
82 | } |
83 | } else { |
84 | if (this.stringToId.equals(objectStringHashMap.stringToId) == false) { |
85 | return false; |
86 | } |
87 | } |
88 | return super.equals(obj); |
89 | } |
90 | |
91 | public Iterator iterator() { |
92 | if (this.idToString == null) { |
93 | return null; |
94 | } |
95 | |
96 | // put them in a treeset first to sort them |
97 | final TreeSet treeSet = new TreeSet(this.idToString.values()); |
98 | if (this.hasEmptyValue) { |
99 | treeSet.add(""); |
100 | } |
101 | return treeSet.iterator(); |
102 | } |
103 | |
104 | public String toString() { |
105 | if (this.value == null) { |
106 | return ""; |
107 | } else if (this.idToString.get(this.value) == null) { |
108 | return ""; |
109 | } else { |
110 | return this.idToString.get(this.value).toString(); |
111 | } |
112 | } |
113 | } |