| 1 | |
package org.jaudiotagger.audio.asf.data; |
| 2 | |
|
| 3 | |
import org.jaudiotagger.audio.asf.util.ChunkPositionComparator; |
| 4 | |
import org.jaudiotagger.audio.asf.util.Utils; |
| 5 | |
|
| 6 | |
import java.math.BigInteger; |
| 7 | |
import java.util.*; |
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | 7 | public class ChunkContainer extends Chunk |
| 15 | |
{ |
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
private final static HashSet<GUID> MULTI_CHUNKS; |
| 22 | |
|
| 23 | |
static |
| 24 | |
{ |
| 25 | 7 | MULTI_CHUNKS = new HashSet<GUID>(); |
| 26 | 7 | MULTI_CHUNKS.add(GUID.GUID_STREAM); |
| 27 | 7 | } |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
private final Map<GUID, List<Chunk>> chunkTable; |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
public ChunkContainer(final GUID chunkGUID, final long pos, final BigInteger length) |
| 41 | |
{ |
| 42 | 160 | super(chunkGUID, pos, length); |
| 43 | 160 | this.chunkTable = new Hashtable<GUID, List<Chunk>>(); |
| 44 | 160 | } |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
public void addChunk(final Chunk toAdd) |
| 51 | |
{ |
| 52 | 325 | List<Chunk> list = assertChunkList(toAdd.getGuid()); |
| 53 | 325 | if (!list.isEmpty() && !MULTI_CHUNKS.contains(toAdd.getGuid())) |
| 54 | |
{ |
| 55 | 0 | throw new IllegalArgumentException("The GUID of the given chunk indicates, that there is no more instance allowed."); |
| 56 | |
} |
| 57 | 325 | list.add(toAdd); |
| 58 | 325 | assert chunkstartsUnique() : "Chunk has equal start position like an already inserted one."; |
| 59 | 325 | } |
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
protected List<Chunk> assertChunkList(final GUID lookFor) |
| 68 | |
{ |
| 69 | 559 | List<Chunk> result = this.chunkTable.get(lookFor); |
| 70 | 559 | if (result == null) |
| 71 | |
{ |
| 72 | 325 | result = new ArrayList<Chunk>(); |
| 73 | 325 | this.chunkTable.put(lookFor, result); |
| 74 | |
} |
| 75 | 559 | return result; |
| 76 | |
} |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
private boolean chunkstartsUnique() |
| 84 | |
{ |
| 85 | 0 | boolean result = true; |
| 86 | 0 | HashSet<Long> chunkStarts = new HashSet<Long>(); |
| 87 | 0 | Collection<Chunk> chunks = getChunks(); |
| 88 | 0 | for (Chunk curr : chunks) |
| 89 | |
{ |
| 90 | 0 | result &= chunkStarts.add(curr.getPosition()); |
| 91 | |
} |
| 92 | 0 | return result; |
| 93 | |
} |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
public Collection<Chunk> getChunks() |
| 101 | |
{ |
| 102 | 0 | final List<Chunk> result = new ArrayList<Chunk>(); |
| 103 | 0 | for (List<Chunk> curr : this.chunkTable.values()) |
| 104 | |
{ |
| 105 | 0 | result.addAll(curr); |
| 106 | |
} |
| 107 | 0 | return result; |
| 108 | |
} |
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
protected Chunk getFirst(GUID lookFor, Class<? extends Chunk> instanceOf) |
| 118 | |
{ |
| 119 | 758 | List<Chunk> list = this.chunkTable.get(lookFor); |
| 120 | 758 | if (list != null && !list.isEmpty()) |
| 121 | |
{ |
| 122 | 565 | Chunk result = list.get(0); |
| 123 | 565 | if (result.getClass().isAssignableFrom(instanceOf)) |
| 124 | |
{ |
| 125 | 565 | return result; |
| 126 | |
} |
| 127 | |
} |
| 128 | 193 | return null; |
| 129 | |
} |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
@Override |
| 135 | |
public String prettyPrint(String prefix) |
| 136 | |
{ |
| 137 | 0 | return prettyPrint(prefix, ""); |
| 138 | |
} |
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
public String prettyPrint(final String prefix, final String containerInfo) |
| 149 | |
{ |
| 150 | 0 | final StringBuffer result = new StringBuffer(super.prettyPrint(prefix)); |
| 151 | 0 | result.append(containerInfo); |
| 152 | 0 | result.append(prefix + " |" + Utils.LINE_SEPARATOR); |
| 153 | 0 | final ArrayList<Chunk> list = new ArrayList<Chunk>(getChunks()); |
| 154 | 0 | Collections.sort(list, new ChunkPositionComparator()); |
| 155 | |
|
| 156 | 0 | for (Chunk curr : list) |
| 157 | |
{ |
| 158 | 0 | result.append(curr.prettyPrint(prefix + " |")); |
| 159 | 0 | result.append(prefix + " |" + Utils.LINE_SEPARATOR); |
| 160 | |
} |
| 161 | 0 | return result.toString(); |
| 162 | |
} |
| 163 | |
} |