01: /*
02: * ====================================================================
03: * Copyright (c) 2004 Marc Strapetz, marc.strapetz@smartsvn.com.
04: * All rights reserved.
05: *
06: * This software is licensed as described in the file COPYING, which
07: * you should have received as part of this distribution. Use is
08: * subject to license terms.
09: * ====================================================================
10: */
11:
12: package de.regnis.q.sequence.media;
13:
14: import java.util.*;
15:
16: import de.regnis.q.sequence.core.*;
17:
18: /**
19: * @author Marc Strapetz
20: */
21: public class QSequenceCachingMediaSymbolMap {
22:
23: // Fields =================================================================
24:
25: private final Map map;
26: private int symbolCount;
27:
28: // Setup ==================================================================
29:
30: public QSequenceCachingMediaSymbolMap(int maximumSize) {
31: this .map = new HashMap(maximumSize);
32: this .symbolCount = 0;
33: }
34:
35: // Accessing ==============================================================
36:
37: public int getSymbolCount() {
38: return symbolCount;
39: }
40:
41: public int[] createSymbols(QSequenceCachableMedia media,
42: QSequenceCachableMediaGetter mediaGetter)
43: throws QSequenceException {
44: final int length = mediaGetter.getMediaLength(media);
45: final int[] symbols = new int[length];
46: for (int index = 0; index < length; index++) {
47: final Object object = mediaGetter.getMediaObject(media,
48: index);
49: symbols[index] = getSymbol(object);
50: }
51: return symbols;
52: }
53:
54: // Utils ==================================================================
55:
56: private int getSymbol(Object obj) {
57: Integer symbol = (Integer) map.get(obj);
58: if (symbol == null) {
59: symbol = new Integer(symbolCount);
60: symbolCount++;
61: map.put(obj, symbol);
62: }
63:
64: return symbol.intValue();
65: }
66: }
|