~ubuntu-branches/ubuntu/utopic/libjaudiotagger-java/utopic

« back to all changes in this revision

Viewing changes to src/org/jaudiotagger/tag/datatype/StringHashMap.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2011-04-28 23:52:43 UTC
  • mfrom: (3.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20110428235243-pzalvw6lncis3ukf
Tags: 2.0.3-1
* d/control: Drop Depends on default-jre per Debian Java Policy as its
  a library package.
* d/watch: Fix to directly monitor SVN tags.
* Switch to 3.0 (quilt) format.
* Bump Standards-Version to 3.9.2 (no changes needed).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 *  @author : Paul Taylor
3
 
 *  @author : Eric Farng
4
 
 *
5
 
 *  Version @version:$Id: StringHashMap.java,v 1.16 2009/11/12 15:42:56 paultaylor Exp $
6
 
 *
7
 
 *  MusicTag Copyright (C)2003,2004
8
 
 *
9
 
 *  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser
10
 
 *  General Public  License as published by the Free Software Foundation; either version 2.1 of the License,
11
 
 *  or (at your option) any later version.
12
 
 *
13
 
 *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
14
 
 *  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
 
 *  See the GNU Lesser General Public License for more details.
16
 
 *
17
 
 *  You should have received a copy of the GNU Lesser General Public License along with this library; if not,
18
 
 *  you can get a copy from http://www.opensource.org/licenses/lgpl-license.php or write to the Free Software
19
 
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
 
 *
21
 
 * Description:
22
 
 *
23
 
 */
24
 
package org.jaudiotagger.tag.datatype;
25
 
 
26
 
import org.jaudiotagger.tag.id3.AbstractTagFrameBody;
27
 
import org.jaudiotagger.tag.id3.valuepair.TextEncoding;
28
 
import org.jaudiotagger.tag.reference.Languages;
29
 
 
30
 
import java.util.Iterator;
31
 
import java.util.Map;
32
 
import java.util.TreeSet;
33
 
 
34
 
 
35
 
/**
36
 
 * Represents a String thats acts as a key into an enumeration of values. The String will be encoded
37
 
 * using the default encoding regardless of what encoding may be specified in the framebody
38
 
 */
39
 
public class StringHashMap extends StringFixedLength implements HashMapInterface<String, String>
40
 
{
41
 
 
42
 
    /**
43
 
     *
44
 
     */
45
 
    Map<String, String> keyToValue = null;
46
 
 
47
 
    /**
48
 
     *
49
 
     */
50
 
    Map<String, String> valueToKey = null;
51
 
 
52
 
    /**
53
 
     *
54
 
     */
55
 
    boolean hasEmptyValue = false;
56
 
 
57
 
    /**
58
 
     * Creates a new ObjectStringHashMap datatype.
59
 
     *
60
 
     * @param identifier
61
 
     * @param frameBody
62
 
     * @param size
63
 
     * @throws IllegalArgumentException
64
 
     */
65
 
    public StringHashMap(String identifier, AbstractTagFrameBody frameBody, int size)
66
 
    {
67
 
        super(identifier, frameBody, size);
68
 
 
69
 
        if (identifier.equals(DataTypes.OBJ_LANGUAGE))
70
 
        {
71
 
            valueToKey = Languages.getInstanceOf().getValueToIdMap();
72
 
            keyToValue = Languages.getInstanceOf().getIdToValueMap();
73
 
        }
74
 
        else
75
 
        {
76
 
            throw new IllegalArgumentException("Hashmap identifier not defined in this class: " + identifier);
77
 
        }
78
 
    }
79
 
 
80
 
    public StringHashMap(StringHashMap copyObject)
81
 
    {
82
 
        super(copyObject);
83
 
 
84
 
        this.hasEmptyValue = copyObject.hasEmptyValue;
85
 
        this.keyToValue = copyObject.keyToValue;
86
 
        this.valueToKey = copyObject.valueToKey;
87
 
    }
88
 
 
89
 
    /**
90
 
     * @return
91
 
     */
92
 
    public Map<String, String> getKeyToValue()
93
 
    {
94
 
        return keyToValue;
95
 
    }
96
 
 
97
 
    /**
98
 
     * @return
99
 
     */
100
 
    public Map<String, String> getValueToKey()
101
 
    {
102
 
        return valueToKey;
103
 
    }
104
 
 
105
 
    /**
106
 
     * @param value
107
 
     */
108
 
    public void setValue(Object value)
109
 
    {
110
 
        if (value instanceof String)
111
 
        {
112
 
            //Issue #273 temporary hack for MM
113
 
            if(value.equals("XXX"))
114
 
            {
115
 
                this.value=value.toString();
116
 
            }
117
 
            else
118
 
            {
119
 
                this.value = ((String) value).toLowerCase();
120
 
            }
121
 
        }
122
 
        else
123
 
        {
124
 
            this.value = value;
125
 
        }
126
 
    }
127
 
 
128
 
    /**
129
 
     * @param obj
130
 
     * @return
131
 
     */
132
 
    public boolean equals(Object obj)
133
 
    {
134
 
        if (!(obj instanceof StringHashMap))
135
 
        {
136
 
            return false;
137
 
        }
138
 
 
139
 
        StringHashMap object = (StringHashMap) obj;
140
 
 
141
 
        if (this.hasEmptyValue != object.hasEmptyValue)
142
 
        {
143
 
            return false;
144
 
        }
145
 
 
146
 
        if (this.keyToValue == null)
147
 
        {
148
 
            if (object.keyToValue != null)
149
 
            {
150
 
                return false;
151
 
            }
152
 
        }
153
 
        else
154
 
        {
155
 
            if (!this.keyToValue.equals(object.keyToValue))
156
 
            {
157
 
                return false;
158
 
            }
159
 
        }
160
 
 
161
 
        if (this.keyToValue == null)
162
 
        {
163
 
            if (object.keyToValue != null)
164
 
            {
165
 
                return false;
166
 
            }
167
 
        }
168
 
        else
169
 
        {
170
 
            if (!this.valueToKey.equals(object.valueToKey))
171
 
            {
172
 
                return false;
173
 
            }
174
 
        }
175
 
 
176
 
        return super.equals(obj);
177
 
    }
178
 
 
179
 
    /**
180
 
     * @return
181
 
     */
182
 
    public Iterator<String> iterator()
183
 
    {
184
 
        if (keyToValue == null)
185
 
        {
186
 
            return null;
187
 
        }
188
 
        else
189
 
        {
190
 
            // put them in a treeset first to sort them
191
 
            TreeSet<String> treeSet = new TreeSet<String>(keyToValue.values());
192
 
 
193
 
            if (hasEmptyValue)
194
 
            {
195
 
                treeSet.add("");
196
 
            }
197
 
 
198
 
            return treeSet.iterator();
199
 
        }
200
 
    }
201
 
 
202
 
    /**
203
 
     * @return
204
 
     */
205
 
    public String toString()
206
 
    {
207
 
        if (value == null)
208
 
        {
209
 
            return "";
210
 
        }
211
 
        else if (keyToValue.get(value) == null)
212
 
        {
213
 
            return "";
214
 
        }
215
 
        else
216
 
        {
217
 
            return keyToValue.get(value);
218
 
        }
219
 
    }
220
 
 
221
 
    /**
222
 
     * @return the ISO_8859 encoding for Datatypes of this type
223
 
     */
224
 
    protected String getTextEncodingCharSet()
225
 
    {
226
 
        return TextEncoding.CHARSET_ISO_8859_1;
227
 
    }
228
 
}
 
1
/**
 
2
 *  @author : Paul Taylor
 
3
 *  @author : Eric Farng
 
4
 *
 
5
 *  Version @version:$Id: StringHashMap.java 836 2009-11-12 15:44:07Z paultaylor $
 
6
 *
 
7
 *  MusicTag Copyright (C)2003,2004
 
8
 *
 
9
 *  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser
 
10
 *  General Public  License as published by the Free Software Foundation; either version 2.1 of the License,
 
11
 *  or (at your option) any later version.
 
12
 *
 
13
 *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 
14
 *  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
15
 *  See the GNU Lesser General Public License for more details.
 
16
 *
 
17
 *  You should have received a copy of the GNU Lesser General Public License along with this library; if not,
 
18
 *  you can get a copy from http://www.opensource.org/licenses/lgpl-license.php or write to the Free Software
 
19
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
20
 *
 
21
 * Description:
 
22
 *
 
23
 */
 
24
package org.jaudiotagger.tag.datatype;
 
25
 
 
26
import org.jaudiotagger.tag.id3.AbstractTagFrameBody;
 
27
import org.jaudiotagger.tag.id3.valuepair.TextEncoding;
 
28
import org.jaudiotagger.tag.reference.Languages;
 
29
 
 
30
import java.util.Iterator;
 
31
import java.util.Map;
 
32
import java.util.TreeSet;
 
33
 
 
34
 
 
35
/**
 
36
 * Represents a String thats acts as a key into an enumeration of values. The String will be encoded
 
37
 * using the default encoding regardless of what encoding may be specified in the framebody
 
38
 */
 
39
public class StringHashMap extends StringFixedLength implements HashMapInterface<String, String>
 
40
{
 
41
 
 
42
    /**
 
43
     *
 
44
     */
 
45
    Map<String, String> keyToValue = null;
 
46
 
 
47
    /**
 
48
     *
 
49
     */
 
50
    Map<String, String> valueToKey = null;
 
51
 
 
52
    /**
 
53
     *
 
54
     */
 
55
    boolean hasEmptyValue = false;
 
56
 
 
57
    /**
 
58
     * Creates a new ObjectStringHashMap datatype.
 
59
     *
 
60
     * @param identifier
 
61
     * @param frameBody
 
62
     * @param size
 
63
     * @throws IllegalArgumentException
 
64
     */
 
65
    public StringHashMap(String identifier, AbstractTagFrameBody frameBody, int size)
 
66
    {
 
67
        super(identifier, frameBody, size);
 
68
 
 
69
        if (identifier.equals(DataTypes.OBJ_LANGUAGE))
 
70
        {
 
71
            valueToKey = Languages.getInstanceOf().getValueToIdMap();
 
72
            keyToValue = Languages.getInstanceOf().getIdToValueMap();
 
73
        }
 
74
        else
 
75
        {
 
76
            throw new IllegalArgumentException("Hashmap identifier not defined in this class: " + identifier);
 
77
        }
 
78
    }
 
79
 
 
80
    public StringHashMap(StringHashMap copyObject)
 
81
    {
 
82
        super(copyObject);
 
83
 
 
84
        this.hasEmptyValue = copyObject.hasEmptyValue;
 
85
        this.keyToValue = copyObject.keyToValue;
 
86
        this.valueToKey = copyObject.valueToKey;
 
87
    }
 
88
 
 
89
    /**
 
90
     * @return
 
91
     */
 
92
    public Map<String, String> getKeyToValue()
 
93
    {
 
94
        return keyToValue;
 
95
    }
 
96
 
 
97
    /**
 
98
     * @return
 
99
     */
 
100
    public Map<String, String> getValueToKey()
 
101
    {
 
102
        return valueToKey;
 
103
    }
 
104
 
 
105
    /**
 
106
     * @param value
 
107
     */
 
108
    public void setValue(Object value)
 
109
    {
 
110
        if (value instanceof String)
 
111
        {
 
112
            //Issue #273 temporary hack for MM
 
113
            if(value.equals("XXX"))
 
114
            {
 
115
                this.value=value.toString();
 
116
            }
 
117
            else
 
118
            {
 
119
                this.value = ((String) value).toLowerCase();
 
120
            }
 
121
        }
 
122
        else
 
123
        {
 
124
            this.value = value;
 
125
        }
 
126
    }
 
127
 
 
128
    /**
 
129
     * @param obj
 
130
     * @return
 
131
     */
 
132
    public boolean equals(Object obj)
 
133
    {
 
134
        if (!(obj instanceof StringHashMap))
 
135
        {
 
136
            return false;
 
137
        }
 
138
 
 
139
        StringHashMap object = (StringHashMap) obj;
 
140
 
 
141
        if (this.hasEmptyValue != object.hasEmptyValue)
 
142
        {
 
143
            return false;
 
144
        }
 
145
 
 
146
        if (this.keyToValue == null)
 
147
        {
 
148
            if (object.keyToValue != null)
 
149
            {
 
150
                return false;
 
151
            }
 
152
        }
 
153
        else
 
154
        {
 
155
            if (!this.keyToValue.equals(object.keyToValue))
 
156
            {
 
157
                return false;
 
158
            }
 
159
        }
 
160
 
 
161
        if (this.keyToValue == null)
 
162
        {
 
163
            if (object.keyToValue != null)
 
164
            {
 
165
                return false;
 
166
            }
 
167
        }
 
168
        else
 
169
        {
 
170
            if (!this.valueToKey.equals(object.valueToKey))
 
171
            {
 
172
                return false;
 
173
            }
 
174
        }
 
175
 
 
176
        return super.equals(obj);
 
177
    }
 
178
 
 
179
    /**
 
180
     * @return
 
181
     */
 
182
    public Iterator<String> iterator()
 
183
    {
 
184
        if (keyToValue == null)
 
185
        {
 
186
            return null;
 
187
        }
 
188
        else
 
189
        {
 
190
            // put them in a treeset first to sort them
 
191
            TreeSet<String> treeSet = new TreeSet<String>(keyToValue.values());
 
192
 
 
193
            if (hasEmptyValue)
 
194
            {
 
195
                treeSet.add("");
 
196
            }
 
197
 
 
198
            return treeSet.iterator();
 
199
        }
 
200
    }
 
201
 
 
202
    /**
 
203
     * @return
 
204
     */
 
205
    public String toString()
 
206
    {
 
207
        if (value == null)
 
208
        {
 
209
            return "";
 
210
        }
 
211
        else if (keyToValue.get(value) == null)
 
212
        {
 
213
            return "";
 
214
        }
 
215
        else
 
216
        {
 
217
            return keyToValue.get(value);
 
218
        }
 
219
    }
 
220
 
 
221
    /**
 
222
     * @return the ISO_8859 encoding for Datatypes of this type
 
223
     */
 
224
    protected String getTextEncodingCharSet()
 
225
    {
 
226
        return TextEncoding.CHARSET_ISO_8859_1;
 
227
    }
 
228
}