Coverage Report - org.jaudiotagger.audio.asf.data.ExtendedContentDescription
 
Classes in this File Line Coverage Branch Coverage Complexity
ExtendedContentDescription
84%
41/49
55%
12/22
0
 
 1  
 /*
 2  
  * Entagged Audio Tag library
 3  
  * Copyright (c) 2004-2005 Christian Laireiter <liree@web.de>
 4  
  * 
 5  
  * This library is free software; you can redistribute it and/or
 6  
  * modify it under the terms of the GNU Lesser General Public
 7  
  * License as published by the Free Software Foundation; either
 8  
  * version 2.1 of the License, or (at your option) any later version.
 9  
  *  
 10  
  * This library is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
  * Lesser General Public License for more details.
 14  
  * 
 15  
  * You should have received a copy of the GNU Lesser General Public
 16  
  * License along with this library; if not, write to the Free Software
 17  
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 18  
  */
 19  
 package org.jaudiotagger.audio.asf.data;
 20  
 
 21  
 import org.jaudiotagger.audio.asf.io.WriteableChunk;
 22  
 import org.jaudiotagger.audio.asf.util.Utils;
 23  
 
 24  
 import java.io.IOException;
 25  
 import java.io.OutputStream;
 26  
 import java.math.BigInteger;
 27  
 import java.util.*;
 28  
 
 29  
 /**
 30  
  * This structure represents the data of a chunk, which contains extended content
 31  
  * description. <br>
 32  
  * These properties are simply represented by
 33  
  * {@link org.jaudiotagger.audio.asf.data.ContentDescriptor}
 34  
  *
 35  
  * @author Christian Laireiter
 36  
  */
 37  5
 public class ExtendedContentDescription extends Chunk implements WriteableChunk
 38  
 {
 39  
 
 40  
     /**
 41  
      * Contains the properties. <br>
 42  
      */
 43  
     private final Map<String, List<ContentDescriptor>> descriptors;
 44  
 
 45  
     /**
 46  
      * Creates an instance.
 47  
      */
 48  
     public ExtendedContentDescription()
 49  
     {
 50  13
         this(BigInteger.valueOf(0));
 51  13
     }
 52  
 
 53  
     /**
 54  
      * Creates an instance.
 55  
      *
 56  
      * @param chunkLen Length of the represented chunck.
 57  
      */
 58  
     public ExtendedContentDescription(BigInteger chunkLen)
 59  
     {
 60  61
         super(GUID.GUID_EXTENDED_CONTENT_DESCRIPTION, chunkLen);
 61  61
         this.descriptors = new LinkedHashMap<String, List<ContentDescriptor>>();
 62  61
     }
 63  
 
 64  
     /**
 65  
      * This method inserts the given ContentDescriptor.
 66  
      *
 67  
      * @param toAdd ContentDescriptor to insert.
 68  
      */
 69  
     public void addDescriptor(ContentDescriptor toAdd)
 70  
     {
 71  1628
         assert toAdd != null : "Argument must not be null.";
 72  1628
         List<ContentDescriptor> list = getDescriptors(toAdd.getName());
 73  1628
         if (list == null)
 74  
         {
 75  1628
             list = new ArrayList<ContentDescriptor>();
 76  1628
             this.descriptors.put(toAdd.getName(), list);
 77  
         }
 78  1628
         list.add(toAdd);
 79  1628
     }
 80  
 
 81  
     /**
 82  
      * This method adds or replaces an existing content descriptor.
 83  
      *
 84  
      * @param descriptor Descriptor to be added or replaced.
 85  
      */
 86  
     public void addOrReplace(ContentDescriptor descriptor)
 87  
     {
 88  365
         assert descriptor != null : "Argument must not be null";
 89  365
         remove(descriptor.getName());
 90  365
         addDescriptor(descriptor);
 91  365
     }
 92  
 
 93  
     /**
 94  
      * Looks if the given <code>fieldName</code> is already contained in this descriptor.<br>
 95  
      * 
 96  
      * @param fieldName name of the field to look for.
 97  
      * @return <code>true</code> if a descriptor with the name is contained.
 98  
      */
 99  
     public boolean containsDescriptor(final String fieldName)
 100  
     {
 101  365
         return this.descriptors.containsKey(fieldName);
 102  
     }
 103  
 
 104  
     /**
 105  
      * {@inheritDoc}
 106  
      */
 107  
     public long getCurrentAsfChunkSize()
 108  
     {
 109  
         /*
 110  
          * 16 bytes GUID, 8  bytes chunk size, 2 bytes descriptor count 
 111  
          */
 112  13
         long result = 26;
 113  13
         for (ContentDescriptor curr : getDescriptors())
 114  
         {
 115  365
             result += curr.getCurrentAsfSize();
 116  
         }
 117  13
         return result;
 118  
     }
 119  
 
 120  
     /**
 121  
      * @return Returns the descriptorCount.
 122  
      */
 123  
     public int getDescriptorCount()
 124  
     {
 125  21
         int result = 0;
 126  21
         for (List<ContentDescriptor> curr : this.descriptors.values())
 127  
         {
 128  720
             result += curr.size();
 129  
         }
 130  21
         return result;
 131  
     }
 132  
 
 133  
     /**
 134  
      * Returns a list of all {@link ContentDescriptor}objects stored in
 135  
      * this extended content description.
 136  
      *
 137  
      * @return A listing of {@link ContentDescriptor}objects.
 138  
      */
 139  
     public List<ContentDescriptor> getDescriptors()
 140  
     {
 141  56
         final ArrayList<ContentDescriptor> result = new ArrayList<ContentDescriptor>();
 142  56
         for (List<ContentDescriptor> curr : this.descriptors.values())
 143  
         {
 144  1585
             result.addAll(curr);
 145  
         }
 146  56
         return result;
 147  
     }
 148  
 
 149  
     /**
 150  
      * Returns a previously inserted content descriptors.
 151  
      *
 152  
      * @param name name of the content descriptor.
 153  
      * @return <code>null</code> if not present.
 154  
      */
 155  
     public List<ContentDescriptor> getDescriptors(String name)
 156  
     {
 157  1658
         return this.descriptors.get(name);
 158  
     }
 159  
 
 160  
     /**
 161  
      * {@inheritDoc}
 162  
      */
 163  
     public boolean isEmpty()
 164  
     {
 165  21
         return getDescriptorCount() == 0;
 166  
     }
 167  
 
 168  
     /**
 169  
      * {@inheritDoc}
 170  
      */
 171  
     public String prettyPrint(final String prefix)
 172  
     {
 173  0
         StringBuffer result = new StringBuffer(super.prettyPrint(prefix));
 174  0
         List<ContentDescriptor> list = getDescriptors();
 175  0
         Collections.sort(list);
 176  0
         for (ContentDescriptor curr : list)
 177  
         {
 178  0
             result.append(prefix + "  |-> ");
 179  0
             result.append(curr);
 180  0
             result.append(Utils.LINE_SEPARATOR);
 181  
         }
 182  0
         return result.toString();
 183  
     }
 184  
 
 185  
     /**
 186  
      * This method removes the content descriptor with the given name. <br>
 187  
      *
 188  
      * @param id The id (name) of the descriptor which should be removed.
 189  
      * @return The descriptors which are removed. If not present <code>null</code>.
 190  
      */
 191  
     public List<ContentDescriptor> remove(String id)
 192  
     {
 193  398
         return this.descriptors.remove(id);
 194  
     }
 195  
 
 196  
     /**
 197  
      * {@inheritDoc}
 198  
      */
 199  
     public long writeInto(OutputStream out) throws IOException
 200  
     {
 201  13
         final long chunkSize = getCurrentAsfChunkSize();
 202  13
         final List<ContentDescriptor> descriptorList = getDescriptors();
 203  13
         out.write(getGuid().getBytes());
 204  13
         Utils.writeUINT64(chunkSize, out);
 205  13
         Utils.writeUINT16(descriptorList.size(), out);
 206  13
         for (ContentDescriptor curr : descriptorList)
 207  
         {
 208  365
             curr.writeInto(out);
 209  
         }
 210  13
         return chunkSize;
 211  
     }
 212  
 }