PersistentSpanSet.java from gzz at Krugle
Show PersistentSpanSet.java syntax highlighted
/*
PersistentSpanSet.java
*
* Copyright (c) 2002, Tuomas Lukka
*
* This file is part of Gzz.
*
* Gzz is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Gzz is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
* Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with Gzz; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
*
*/
/*
* Written by Marc Schiereck
*/
package gzz.media.impl;
import gzz.mediaserver.*;
import gzz.media.*;
import java.util.*;
import java.io.*;
import gzz.*;
import gzz.mediaserver.storage.*;
public class PersistentSpanSet implements SpanSet {
String rcsid = "$Id: PersistentSpanSet.java,v 1.3 2003/02/21 20:02:50 tjl Exp $";
public interface ObjectFormat {
Object read(Reader r);
Span readSpan(Reader r);
void write(Writer w, Span s, Object o);
}
Map map = new HashMap();
ObjectFormat format;
Storer storer;
class SpanAndObject {
String id;
List spans = new ArrayList();
List objs = new ArrayList();
void add(Span span, Object obj) {
this.spans.add(span);
this.objs.add(obj);
}
}
public PersistentSpanSet(Storer storer, ObjectFormat format) {
this.format = format;
this.storer = storer;
}
public void addSpan(Span span, Object obj) {
String id = span.getScrollBlock().getID();
if (map.get(id) == null) {
SpanAndObject sao = readSAO(id);
if (sao == null) {
sao = new SpanAndObject();
sao.id = id;
}
sao.add(span,obj);
map.put(id, sao);
}
else {
((SpanAndObject)map.get(id)).add(span,obj);
}
}
public Collection overlaps(Span span) {
String id = span.getScrollBlock().getID();
List res = new ArrayList();
if (map.get(id) != null) {
SpanAndObject sao = (SpanAndObject)map.get(id);
for(int i=0;i<sao.spans.size();i++) {
if(((Span)sao.spans.get(i)).intersects(span))
res.add(sao.objs.get(i));
}
}
return res;
}
public Collection spans() {
Iterator iter = map.values().iterator();
List res = new ArrayList();
while(iter.hasNext()) {
res.addAll(((SpanAndObject)iter.next()).spans);
}
return Collections.unmodifiableCollection(res);
}
public void write () throws IOException {
Iterator iter = map.values().iterator();
while(iter.hasNext()) {
writeSAO((SpanAndObject)iter.next());
}
}
protected void writeSAO(SpanAndObject sao) throws IOException{
Writer write = new OutputStreamWriter(storer.store("pss_"+sao.id));
Iterator iter1, iter2;
iter1 = sao.spans.iterator();
iter2 = sao.objs.iterator();
while(iter1.hasNext()) {
format.write(write, (Span)iter1.next(),iter2.next());
}
}
SpanAndObject readSAO(String id) {
InputStream is;
try {
is = storer.retrieve("pss_"+id);
}
catch(IOException e) {
return null;
}
if(is == null)
return new SpanAndObject();
Reader reader = new InputStreamReader(is);
SpanAndObject sao = new SpanAndObject();
sao.id = id;
Span span;
Object object;
System.out.println("my format is: "+format);
while((span = format.readSpan(reader)) != null) {
object = format.read(reader);
sao.add(span, object);
}
return sao;
}
}
See more files for this project here