📄 abstracttag.java
字号:
/*
* jaudiotagger library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.hadeslee.audiotag.audio.generic;
import com.hadeslee.audiotag.tag.FieldDataInvalidException;
import com.hadeslee.audiotag.tag.KeyNotFoundException;
import com.hadeslee.audiotag.tag.Tag;
import com.hadeslee.audiotag.tag.TagField;
import com.hadeslee.audiotag.tag.TagFieldKey;
import com.hadeslee.audiotag.tag.TagTextField;
import java.util.*;
import java.util.List;
/**
* This class is the default implementation for
* {@link com.hadeslee.jaudiotagger.tag.Tag} and introduces some more useful
* functionality to be implemented.<br>
*
* @author Rapha�l Slinckx
*/
public abstract class AbstractTag implements Tag
{
/**
* Stores the amount of {@link TagField} with {@link TagField#isCommon()}
* <code>true</code>.
*/
protected int commonNumber = 0;
/**
* This map stores the {@linkplain TagField#getId() ids} of the stored
* fields to the {@linkplain TagField fields} themselves. Because a linked hashMap is used the order
* that they are added in is preserved, the only exception to this rule is when two fields of the same id
* exist, both will be returned according to when the first item was added to the file. <br>
*/
protected Map<String,List<TagField>> fields = new LinkedHashMap<String,List<TagField>>();
/**
* Add field
*
* @see com.hadeslee.jaudiotagger.tagcom.hadeslee.jaudiotaggerdiotagger.tag.TagField)
* <p/>
* Changed so add empty fields
*/
public void add(TagField field)
{
if (field == null)
{
return;
}
List list = fields.get(field.getId());
// There was no previous item
if (list == null)
{
list = new ArrayList();
list.add(field);
fields.put(field.getId(), list);
if (field.isCommon())
{
commonNumber++;
}
}
else
{
// We append to existing list
list.add(field);
}
}
/**
* Add (another) album
*
* @see com.hadeslee.jaudiotagger.tag.Tag#addAlbum(java.lang.String)
*/
public void addAlbum(String s)
{
add(createAlbumField(s));
}
/**
* Add (another) artist
*
* @see com.hadeslee.jaudiotagger.tag.Tag#addArtist(java.lang.String)
*/
public void addArtist(String s)
{
add(createArtistField(s));
}
/**
* Add (another) comment
*
* @see com.hadeslee.jaudiotagger.tag.Tag#addComment(java.lang.String)
*/
public void addComment(String s)
{
add(createCommentField(s));
}
/**
* Add (another) genre
*
* @see com.hadeslee.jaudiotagger.tag.Tag#addGenre(java.lang.String)
*/
public void addGenre(String s)
{
add(createGenreField(s));
}
/**
* Add (another) title
*
* @see com.hadeslee.jaudiotagger.tag.Tag#addTitle(java.lang.String)
*/
public void addTitle(String s)
{
add(createTitleField(s));
}
/**
* Add (another) track
*
* @see com.hadeslee.jaudiotagger.tag.Tag#addTrack(java.lang.String)
*/
public void addTrack(String s) throws FieldDataInvalidException
{
add(createTrackField(s));
}
/**
* (Add (another) year
*
* @see com.hadeslee.jaudiotagger.tag.Tag#addYear(java.lang.String)
*/
public void addYear(String s)
{
add(createYearField(s));
}
/**
* Get list of fields within this tag with the specified id
*
* @see com.hadeslee.jaudiotagger.tag.Tag#get(java.lang.String)
*/
public List<TagField> get(String id)
{
List<TagField> list = fields.get(id);
if (list == null)
{
return new ArrayList<TagField>();
}
return list;
}
/**
* @param id
* @return
*/
//Needs to be overridden
//TODO remove
public List<TagField> get(TagFieldKey id) throws KeyNotFoundException
{
List<TagField> list = fields.get(id.name());
if (list == null)
{
return new ArrayList<TagField> ();
}
return list;
}
/**
*
* @param id
* @return matches for this saudio-specific key
*/
public String getFirst(String id)
{
List l = get(id);
return (l.size() != 0) ? l.get(0).toString():"";
}
public TagField getFirstField(String id)
{
List<TagField>l = get(id);
return (l.size() != 0) ? ((TagField) l.get(0)):null;
}
/**
* (overridden)
*
* @see com.hadeslee.jaudiotagger.tag.Tag#getAlbum()
*/
public List getAlbum()
{
return get(getAlbumId());
}
/**
* Returns the identifier for a field representing the "album"<br>
*
* @return identifier for the "album" field.
* @see TagField#getId()
*/
protected abstract String getAlbumId();
/**
* Get Artist
*
* @see com.hadeslee.jaudiotagger.tag.Tag#getArtist()
*/
public List getArtist()
{
return get(getArtistId());
}
/**
* Returns the identifier for a field representing the "artist"<br>
*
* @return identifier for the "artist" field.
* @see TagField#getId()
*/
protected abstract String getArtistId();
/**
* Get Comment
*
* @see com.hadeslee.jaudiotagger.tag.Tag#getComment()
*/
public List getComment()
{
return get(getCommentId());
}
/**
* Returns the identifier for a field representing the "comment"<br>
*
* @return identifier for the "comment" field.
* @see TagField#getId()
*/
protected abstract String getCommentId();
/**
* @see com.hadeslee.jaudiotaggerdiotagger.tag.Tag#getFields()
*/
public Iterator getFields()
{
final Iterator<Map.Entry<String,List<TagField>>> it = this.fields.entrySet().iterator();
return new Iterator()
{
private Iterator fieldsIt;
private void changeIt()
{
if (!it.hasNext())
{
return;
}
Map.Entry<String,List<TagField>> e = it.next();
List<TagField> l = e.getValue();
fieldsIt = l.iterator();
}
public boolean hasNext()
{
if (fieldsIt == null)
{
changeIt();
}
return it.hasNext() || (fieldsIt != null && fieldsIt.hasNext());
}
public Object next()
{
if (!fieldsIt.hasNext())
{
changeIt();
}
return fieldsIt.next();
}
public void remove()
{
fieldsIt.remove();
}
};
}
/**
* Return field count
*
* TODO:There must be a more efficient way to do this.
*
* @return field count
*/
public int getFieldCount()
{
Iterator it = getFields();
int count = 0;
while(it.hasNext())
{
count ++;
it.next();
}
return count;
}
/**
* Get the first album or empty string if doesnt exist
*
* @see com.hadeslee.jaudiotagger.tag.Tag#getFirstAlbum()
*/
public String getFirstAlbum()
{
List l = getAlbum();
return (l.size() != 0) ? ((TagTextField) l.get(0)).getContent() : "";
}
/**
* Get the first artist or empty string if doesnt exist
*
* @see com.hadeslee.jaudiotagger.tag.Tag#getFirstArtist()
*/
public String getFirstArtist()
{
List l = getArtist();
return (l.size() != 0) ? ((TagTextField) l.get(0)).getContent() : "";
}
/**
* Get the first comment or empty string if doesnt exist
*
* @see com.hadeslee.jaudiotagger.tag.Tag#getFirstComment()
*/
public String getFirstComment()
{
List l = getComment();
return (l.size() != 0) ? ((TagTextField) l.get(0)).getContent() : "";
}
/**
* Get the first genre or empty string if doesnt exist
*
* @see com.hadeslee.jaudiotagger.tag.Tag#getFirstGenre()
*/
public String getFirstGenre()
{
List l = getGenre();
return (l.size() != 0) ? ((TagTextField) l.get(0)).getContent() : "";
}
/**
* Get the first title or empty string if doesnt exist
*
* @see com.hadeslee.jaudiotagger.tag.Tag#getFirstTitle()
*/
public String getFirstTitle()
{
List l = getTitle();
return (l.size() != 0) ? ((TagTextField) l.get(0)).getContent() : "";
}
/**
* Get the first track or empty string if doesnt exist
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -