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 ObjectNumberHashMap extends ObjectNumberFixedLength implements ObjectHashMapInterface { |
17 | |
18 | public static final String GENRE = "Genre"; |
19 | public static final String TEXT_ENCODING = "Text Encoding"; |
20 | public static final String INTERPOLATION_METHOD = "Interpolation Method"; |
21 | public static final String ID3V2_FRAME_DESCRIPTION = "ID3v2 Frame Description"; |
22 | public static final String PICTURE_TYPE = "Picture Type"; |
23 | public static final String TYPE_OF_EVENT = "Type Of Event"; |
24 | public static final String TIME_STAMP_FORMAT = "Time Stamp Format"; |
25 | public static final String TYPE_OF_CHANNEL = "Type Of Channel"; |
26 | public static final String RECIEVED_AS = "Recieved As"; |
27 | private HashMap idToString = null; |
28 | private HashMap stringToId = null; |
29 | private boolean hasEmptyValue = false; |
30 | |
31 | /** |
32 | * Creates a new ObjectNumberHashMap object. |
33 | */ |
34 | public ObjectNumberHashMap(final String identifier, final int size) { |
35 | super(identifier, size); |
36 | if (identifier.equals(ObjectNumberHashMap.GENRE)) { |
37 | this.stringToId = TagConstant.genreStringToId; |
38 | this.idToString = TagConstant.genreIdToString; |
39 | this.hasEmptyValue = true; |
40 | } else if (identifier.equals(ObjectNumberHashMap.TEXT_ENCODING)) { |
41 | this.stringToId = TagConstant.textEncodingStringToId; |
42 | this.idToString = TagConstant.textEncodingIdToString; |
43 | } else if (identifier.equals(ObjectNumberHashMap.INTERPOLATION_METHOD)) { |
44 | this.stringToId = TagConstant.interpolationMethodStringToId; |
45 | this.idToString = TagConstant.interpolationMethodIdToString; |
46 | } else if (identifier.equals(ObjectNumberHashMap.ID3V2_FRAME_DESCRIPTION)) { |
47 | this.stringToId = TagConstant.id3v2_4FrameStringToId; |
48 | this.idToString = TagConstant.id3v2_4FrameIdToString; |
49 | } else if (identifier.equals(ObjectNumberHashMap.PICTURE_TYPE)) { |
50 | this.stringToId = TagConstant.pictureTypeStringToId; |
51 | this.idToString = TagConstant.pictureTypeIdToString; |
52 | } else if (identifier.equals(ObjectNumberHashMap.TYPE_OF_EVENT)) { |
53 | this.stringToId = TagConstant.typeOfEventStringToId; |
54 | this.idToString = TagConstant.typeOfEventIdToString; |
55 | } else if (identifier.equals(ObjectNumberHashMap.TIME_STAMP_FORMAT)) { |
56 | this.stringToId = TagConstant.timeStampFormatStringToId; |
57 | this.idToString = TagConstant.timeStampFormatIdToString; |
58 | } else if (identifier.equals(ObjectNumberHashMap.TYPE_OF_CHANNEL)) { |
59 | this.stringToId = TagConstant.typeOfChannelStringToId; |
60 | this.idToString = TagConstant.typeOfChannelIdToString; |
61 | } else if (identifier.equals(ObjectNumberHashMap.RECIEVED_AS)) { |
62 | this.stringToId = TagConstant.recievedAsStringToId; |
63 | this.idToString = TagConstant.recievedAsIdToString; |
64 | } else { |
65 | throw new IllegalArgumentException("Hashmap identifier not defined in this class: " + identifier); |
66 | } |
67 | } |
68 | |
69 | /** |
70 | * Creates a new ObjectNumberHashMap object. |
71 | */ |
72 | public ObjectNumberHashMap(final ObjectNumberHashMap copyObject) { |
73 | super(copyObject); |
74 | this.hasEmptyValue = copyObject.hasEmptyValue; |
75 | |
76 | // we dont' need to clone/copy the maps here because they are static |
77 | this.idToString = copyObject.idToString; |
78 | this.stringToId = copyObject.stringToId; |
79 | } |
80 | |
81 | public HashMap getIdToString() { |
82 | return this.idToString; |
83 | } |
84 | |
85 | public HashMap getStringToId() { |
86 | return this.stringToId; |
87 | } |
88 | |
89 | public void setValue(final Object value) { |
90 | if (value instanceof Byte) { |
91 | this.value = new Long(((Byte) value).byteValue()); |
92 | } else if (value instanceof Short) { |
93 | this.value = new Long(((Short) value).shortValue()); |
94 | } else if (value instanceof Integer) { |
95 | this.value = new Long(((Integer) value).intValue()); |
96 | } else { |
97 | this.value = value; |
98 | } |
99 | } |
100 | |
101 | public boolean equals(final Object obj) { |
102 | if ((obj instanceof ObjectNumberHashMap) == false) { |
103 | return false; |
104 | } |
105 | final ObjectNumberHashMap objectNumberHashMap = (ObjectNumberHashMap) obj; |
106 | if (this.hasEmptyValue != objectNumberHashMap.hasEmptyValue) { |
107 | return false; |
108 | } |
109 | if (this.idToString == null) { |
110 | if (objectNumberHashMap.idToString != null) { |
111 | return false; |
112 | } |
113 | } else { |
114 | if (this.idToString.equals(objectNumberHashMap.idToString) == false) { |
115 | return false; |
116 | } |
117 | } |
118 | if (this.stringToId == null) { |
119 | if (objectNumberHashMap.stringToId != null) { |
120 | return false; |
121 | } |
122 | } else { |
123 | if (this.stringToId.equals(objectNumberHashMap.stringToId) == false) { |
124 | return false; |
125 | } |
126 | } |
127 | return super.equals(obj); |
128 | } |
129 | |
130 | public Iterator iterator() { |
131 | if (this.idToString == null) { |
132 | return null; |
133 | } |
134 | |
135 | // put them in a treeset first to sort them |
136 | final TreeSet treeSet = new TreeSet(this.idToString.values()); |
137 | if (this.hasEmptyValue) { |
138 | treeSet.add(""); |
139 | } |
140 | return treeSet.iterator(); |
141 | } |
142 | |
143 | public String toString() { |
144 | if (this.value == null) { |
145 | return ""; |
146 | } else if (this.idToString.get(this.value) == null) { |
147 | return ""; |
148 | } else { |
149 | return this.idToString.get(this.value).toString(); |
150 | } |
151 | } |
152 | } |