001: package it.unimi.dsi.mg4j.index;
002:
003: /*
004: * MG4J: Managing Gigabytes for Java
005: *
006: * Copyright (C) 2006-2007 Sebastiano Vigna
007: *
008: * This library is free software; you can redistribute it and/or modify it
009: * under the terms of the GNU Lesser General Public License as published by the Free
010: * Software Foundation; either version 2.1 of the License, or (at your option)
011: * any later version.
012: *
013: * This library is distributed in the hope that it will be useful, but
014: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
015: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
016: * for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: *
022: */
023:
024: import it.unimi.dsi.mg4j.search.Interval;
025: import it.unimi.dsi.util.StringMap;
026: import it.unimi.dsi.mg4j.util.MutableString;
027:
028: import java.io.Serializable;
029:
030: /** A class providing static methods and objects that do useful things with
031: * {@linkplain TermMap term maps} and {@linkplain PrefixMap prefix maps}.
032: *
033: * @see TermMap
034: * @see PrefixMap
035: * @author Sebastiano Vigna
036: * @deprecated Use {@link StringMap} and related classes.
037: */
038:
039: @Deprecated
040: public class TermMaps {
041: private TermMaps() {
042: }
043:
044: protected static class SynchronizedTermMap implements TermMap,
045: Serializable {
046: private static final long serialVersionUID = 1L;
047: protected final TermMap termMap;
048:
049: public SynchronizedTermMap(final TermMap termMap) {
050: this .termMap = termMap;
051: }
052:
053: public synchronized int size() {
054: return termMap.size();
055: }
056:
057: public synchronized int getNumber(CharSequence term) {
058: return termMap.getNumber(term);
059: }
060:
061: public synchronized MutableString getTerm(int index,
062: MutableString term) {
063: return termMap.getTerm(index, term);
064: }
065:
066: public synchronized CharSequence getTerm(int index) {
067: return termMap.getTerm(index);
068: }
069:
070: public boolean hasTerms() {
071: return termMap.hasTerms();
072: }
073: }
074:
075: protected static class SynchronizedPrefixMap implements PrefixMap,
076: Serializable {
077: private static final long serialVersionUID = 1L;
078: protected final PrefixMap prefixMap;
079:
080: public SynchronizedPrefixMap(final PrefixMap prefixMap) {
081: this .prefixMap = prefixMap;
082: }
083:
084: public synchronized Interval getInterval(CharSequence prefix) {
085: return prefixMap.getInterval(prefix);
086: }
087:
088: public synchronized MutableString getPrefix(Interval interval,
089: MutableString prefix) {
090: return prefixMap.getPrefix(interval, prefix);
091: }
092:
093: public synchronized CharSequence getPrefix(Interval interval) {
094: return prefixMap.getPrefix(interval);
095: }
096:
097: public synchronized int size() {
098: return prefixMap.size();
099: }
100:
101: public boolean hasPrefixes() {
102: return prefixMap.hasPrefixes();
103: }
104: }
105:
106: protected static class SynchronizedTermPrefixMap extends
107: SynchronizedTermMap implements PrefixMap {
108: private static final long serialVersionUID = 1L;
109: private final PrefixMap prefixMap;
110:
111: public SynchronizedTermPrefixMap(final Object termPrefixMap) {
112: super ((TermMap) termPrefixMap);
113: this .prefixMap = (PrefixMap) termPrefixMap;
114: }
115:
116: public synchronized Interval getInterval(CharSequence prefix) {
117: return prefixMap.getInterval(prefix);
118: }
119:
120: public synchronized MutableString getPrefix(Interval interval,
121: MutableString prefix) {
122: return prefixMap.getPrefix(interval, prefix);
123: }
124:
125: public synchronized CharSequence getPrefix(Interval interval) {
126: return prefixMap.getPrefix(interval);
127: }
128:
129: public boolean hasPrefixes() {
130: return prefixMap.hasPrefixes();
131: }
132: }
133:
134: /** Returns a synchronized term map backed by the given term map.
135: *
136: * <p>If the provided term map implements also {@link PrefixMap},
137: * the synchronized view will, too.
138: *
139: * @param termMap the term map to be wrapped in a synchronized map.
140: * @return a synchronized view of the specified term map.
141: */
142: public static TermMap synchronize(final TermMap termMap) {
143: if (termMap instanceof PrefixMap)
144: return new SynchronizedTermPrefixMap(termMap);
145: return new SynchronizedTermMap(termMap);
146: }
147:
148: /** Returns a synchronized prefix map backed by the given prefix map.
149: *
150: * <p>If the provided prefix map implements also {@link TermMap},
151: * the synchronized view will, too.
152: *
153: * @param prefixMap the prefix map to be wrapped in a synchronized map.
154: * @return a synchronized view of the specified prefix map.
155: */
156: public static PrefixMap synchronize(final PrefixMap prefixMap) {
157: if (prefixMap instanceof TermMap)
158: return new SynchronizedTermPrefixMap(prefixMap);
159: return new SynchronizedPrefixMap(prefixMap);
160: }
161: }
|