FileDocCategorySizeDatePackage
AbstractTag.javaAPI DocJaudiotagger 2.0.412322Wed Dec 07 10:21:26 GMT 2011org.jaudiotagger.audio.generic

AbstractTag

public abstract class AbstractTag extends Object implements Tag
This class is the default implementation for {@link org.jaudiotagger.tag.Tag} and introduces some more useful functionality to be implemented.
author
Raphaƫl Slinckx

Fields Summary
protected int
commonNumber
Stores the amount of {@link TagField} with {@link TagField#isCommon()} true.
protected Map
fields
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.
Constructors Summary
Methods Summary
public voidaddField(TagField field)
Add field

see
org.jaudiotagger.tag.Tag#addField(org.jaudiotagger.tag.TagField)

Changed so add empty fields


                             
       
    
        if (field == null)
        {
            return;
        }

        List<TagField> list = fields.get(field.getId());

        // There was no previous item
        if (list == null)
        {
            list = new ArrayList<TagField>();
            list.add(field);
            fields.put(field.getId(), list);
            if (field.isCommon())
            {
                commonNumber++;
            }
        }
        else
        {
            // We append to existing list
            list.add(field);
        }
    
public voidaddField(FieldKey genericKey, java.lang.String value)
Create new field and add it to the tag

param
genericKey
param
value
throws
KeyNotFoundException
throws
FieldDataInvalidException

        TagField tagfield = createField(genericKey,value);
        addField(tagfield);
    
public voidaddField(org.jaudiotagger.tag.images.Artwork artwork)
Create field and then add within tag itself

param
artwork
throws
FieldDataInvalidException

       this.addField(createField(artwork));
    
public abstract TagFieldcreateField(FieldKey genericKey, java.lang.String value)

param
genericKey
param
value
return
throws
KeyNotFoundException
throws
FieldDataInvalidException

public voiddeleteArtworkField()
Delete all instance of artwork Field

throws
KeyNotFoundException

        this.deleteField(FieldKey.COVER_ART);
    
public abstract voiddeleteField(FieldKey fieldKey)

param
fieldKey
throws
KeyNotFoundException

public voiddeleteField(java.lang.String key)
Delete all occurrences of field with this id.

param
key

        fields.remove(key);
    
public intgetFieldCount()
Return field count

TODO:There must be a more efficient way to do this.

return
field count

        Iterator it = getFields();
        int count = 0;
        while (it.hasNext())
        {
            count++;
            it.next();
        }
        return count;
    
public intgetFieldCountIncludingSubValues()

        return getFieldCount();
    
public java.util.ListgetFields(java.lang.String id)
Get list of fields within this tag with the specified id

see
org.jaudiotagger.tag.Tag#getFields(java.lang.String)

        List<TagField> list = fields.get(id);

        if (list == null)
        {
            return new ArrayList<TagField>();
        }

        return list;
    
public java.util.ListgetFields(FieldKey id)

param
id
return

        List<TagField> list = fields.get(id.name());
        if (list == null)
        {
            return new ArrayList<TagField>();
        }
        return list;
    
public java.util.IteratorgetFields()

see
org.jaudiotagger.tag.Tag#getFields()

        final Iterator<Map.Entry<String, List<TagField>>> it = this.fields.entrySet().iterator();
        return new Iterator<TagField>()
        {
            private Iterator<TagField> 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 TagField next()
            {
                if (!fieldsIt.hasNext())
                {
                    changeIt();
                }

                return fieldsIt.next();
            }

            public void remove()
            {
                fieldsIt.remove();
            }
        };
    
public java.lang.StringgetFirst(FieldKey genericKey)
Retrieve the first value that exists for this generic key

param
genericKey
return

        return getValue(genericKey,0);
    
public java.lang.StringgetFirst(java.lang.String id)

param
id
return

        List<TagField> l = getFields(id);
        return (l.size() != 0) ? l.get(0).toString() : "";
    
public org.jaudiotagger.tag.images.ArtworkgetFirstArtwork()

        List<Artwork> artwork = getArtworkList();
        if(artwork.size()>0)
        {
            return artwork.get(0);
        }
        return null;
    
public abstract TagFieldgetFirstField(FieldKey genericKey)

param
genericKey
return
throws
KeyNotFoundException

public TagFieldgetFirstField(java.lang.String id)

param
id audio specific key
return

        List<TagField> l = getFields(id);
        return (l.size() != 0) ? l.get(0) : null;
    
public java.lang.StringgetItem(java.lang.String id, int index)

param
id
param
index
return

        List<TagField> l = getFields(id);
        return (l.size()>index) ? l.get(index).toString() : "";
    
public java.lang.StringgetSubValue(FieldKey id, int n, int m)
The m parameter is effectively ignored

param
id
param
n
param
m
return

        return getValue(id,n);
    
public booleanhasCommonFields()
Does this tag contain any comon fields

see
org.jaudiotagger.tag.Tag#hasCommonFields()

        return commonNumber != 0;
    
public booleanhasField(java.lang.String id)
Does this tag contain a field with the specified id

see
org.jaudiotagger.tag.Tag#hasField(java.lang.String)

        return getFields(id).size() != 0;
    
public booleanhasField(FieldKey fieldKey)

        return hasField(fieldKey.name());
    
protected abstract booleanisAllowedEncoding(java.lang.String enc)
Determines whether the given charset encoding may be used for the represented tagging system.

param
enc charset encoding.
return
true if the given encoding can be used.

public booleanisEmpty()
Is this tag empty

see
org.jaudiotagger.tag.Tag#isEmpty()

        return fields.size() == 0;
    
public booleansetEncoding(java.lang.String enc)
Set or add encoding

see
org.jaudiotagger.tag.Tag#setEncoding(java.lang.String)

        if (!isAllowedEncoding(enc))
        {
            return false;
        }

        Iterator it = getFields();
        while (it.hasNext())
        {
            TagField field = (TagField) it.next();
            if (field instanceof TagTextField)
            {
                ((TagTextField) field).setEncoding(enc);
            }
        }

        return true;
    
public voidsetField(FieldKey genericKey, java.lang.String value)
Create new field and set it in the tag

param
genericKey
param
value
throws
KeyNotFoundException
throws
FieldDataInvalidException

        TagField tagfield = createField(genericKey,value);
        setField(tagfield);
    
public voidsetField(TagField field)
Set field

Changed:Just because field is empty it doesn't mean it should be deleted. That should be the choice of the developer. (Or does this break things)

see
org.jaudiotagger.tag.Tag#setField(org.jaudiotagger.tag.TagField)

        if (field == null)
        {
            return;
        }

        // If there is already an existing field with same id
        // and both are TextFields, we replace the first element
        List<TagField> list = fields.get(field.getId());
        if (list != null)
        {
            list.set(0, field);
            return;
        }

        // Else we put the new field in the fields.
        list = new ArrayList<TagField>();
        list.add(field);
        fields.put(field.getId(), list);
        if (field.isCommon())
        {
            commonNumber++;
        }
    
public voidsetField(org.jaudiotagger.tag.images.Artwork artwork)
Create field and then set within tag itself

param
artwork
throws
FieldDataInvalidException

        this.setField(createField(artwork));
    
public java.lang.StringtoString()
(overridden)

see
java.lang.Object#toString()

        StringBuffer out = new StringBuffer();
        out.append("Tag content:\n");
        Iterator it = getFields();
        while (it.hasNext())
        {
            TagField field = (TagField) it.next();
            out.append("\t");
            out.append(field.getId());
            out.append(":");
            out.append(field.toString());
            out.append("\n");
        }
        return out.toString().substring(0, out.length() - 1);