📄 timeorderimpl.java
字号:
/* * File: TimeOrderImpl.java * Project: MPI Linguistic Application * Date: 02 May 2007 * * Copyright (C) 2001-2007 Max Planck Institute for Psycholinguistics * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package mpi.eudico.server.corpora.clomimpl.abstr;import mpi.eudico.server.corpora.clom.TimeOrder;import mpi.eudico.server.corpora.clom.TimeSlot;import mpi.eudico.server.corpora.clom.Transcription;import java.util.ArrayList;import java.util.Enumeration;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Vector;/** * TimeOrder encapsulates the ordering of TimeSlots in a Transcription. It is * considered to be part of the Transcription. The TimeOrder is used when * comparing TimeSlots in the TimeSlot's compareTo method. Given a constructed * TimeOrder, it is then sufficient to add TimeSlots to a TreeSet, they will * be ordered according to the TimeOrder automatically. * * @author Hennie Brugman */public class TimeOrderImpl implements TimeOrder { private Vector orderedTimeSlotList; private Transcription transcription; /** * Creates a new TimeOrderImpl instance * * @param theTranscription DOCUMENT ME! */ public TimeOrderImpl(Transcription theTranscription) { transcription = theTranscription; orderedTimeSlotList = new Vector(); } /** * Adds a TimeSlot to the TimeOrder at the latest possible position. The * TimeSlot can be either time-aligned or not time-aligned. * * @param theTimeSlot the TimeSlot to be inserted. */ public void insertTimeSlot(TimeSlot theTimeSlot) { if (theTimeSlot.isTimeAligned()) { int index = 0; long time = theTimeSlot.getTime(); if (orderedTimeSlotList.size() > 0) { Iterator tsIter = orderedTimeSlotList.iterator(); while (tsIter.hasNext()) { if (((TimeSlot) tsIter.next()).getTime() > time) { break; } index++; } } orderedTimeSlotList.insertElementAt(theTimeSlot, index); reindex(index); } else { // not time aligned orderedTimeSlotList.add(theTimeSlot); // at end reindex(orderedTimeSlotList.size() - 1); } //reindex(); } private void reindex() { int i = 0; Iterator tsIter = orderedTimeSlotList.iterator(); while (tsIter.hasNext()) { ((TimeSlot) tsIter.next()).setIndex(i++); } } /** * A refined version of reindex: change the index of the slots from a certain * point, e.g. after insertion of a slot at a certain index. * * @param fromIndex the index to start reindexing from */ private void reindex(int fromIndex) { for (int i = fromIndex; i < orderedTimeSlotList.size(); i++) { ((TimeSlot) orderedTimeSlotList.get(i)).setIndex(i); } /* System.out.print("Size: " + orderedTimeSlotList.size() + " from: " + fromIndex); if (orderedTimeSlotList.size() > 0) { System.out.print(" first: " + ((TimeSlot) orderedTimeSlotList.get(0)).getIndex()); System.out.println(" last: " + ((TimeSlot) orderedTimeSlotList.get( orderedTimeSlotList.size() - 1)).getIndex()); } */ } /** * Adds a TimeSlot to the TimeOrder at current position. The TimeSlot can * be either time-aligned or not time-aligned. The TimeSlot is inserted * after 'afterSlot' and before 'beforeSlot'. * * @param theTimeSlot the TimeSlot to be inserted. * @param afterSlot DOCUMENT ME! * @param beforeSlot DOCUMENT ME! */ public void insertTimeSlot(TimeSlot theTimeSlot, TimeSlot afterSlot, TimeSlot beforeSlot) { int index = 0; boolean positioned = false; long time = theTimeSlot.getTime(); // iterate until afterSlot Iterator tsIter = orderedTimeSlotList.iterator(); while (tsIter.hasNext()) { index++; if ((TimeSlot) tsIter.next() == afterSlot) { break; } } if (!tsIter.hasNext()) { // at end of time order positioned = true; } // iterate until time > theTimeSlot's time, or until beforeSlot is reached while (tsIter.hasNext()) { TimeSlot ts = (TimeSlot) tsIter.next(); if ((ts.isTimeAligned()) && (ts.getTime() > time)) { positioned = true; // if (!theTimeSlot.isTimeAligned() && (beforeSlot == null)) { // AD HOC !!!??? // index++; // } break; } else { if (!theTimeSlot.isTimeAligned() && (beforeSlot == null)) { // AD HOC !!!??? // index++; positioned = true; break; } } if (ts == beforeSlot) { if (!ts.isTimeAligned()) { positioned = true; } break; } index++; } // insert tsIter = null; // to prevent ConcurrentModificationExceptions if (positioned) { orderedTimeSlotList.insertElementAt(theTimeSlot, index); reindex(index); } else { System.out.println("Not positioned..."); reindex(); } // reindex //reindex(); } /** * DOCUMENT ME! * * @param theSlot DOCUMENT ME! */ public void removeTimeSlot(TimeSlot theSlot) { orderedTimeSlotList.removeElement(theSlot); reindex(); } /** * A utility method to print the current state of TimeOrder to standard * output. */ public void printTimeOrder() { System.out.println(""); Iterator iter = orderedTimeSlotList.iterator(); while (iter.hasNext()) { TimeSlot t = (TimeSlot) iter.next(); System.out.println(t.getIndex() + " " + t.getTime()); } } /** * Returns true if timeSlot1 starts before timeSlot2, according to the * order specified by the TimeOrder. Each TimeSlot can be either * time-aligned or not time-aligned. * * @param timeSlot1 first TimeSlot to be compared. * @param timeSlot2 second TimeSlot to be compared. * * @return true if timeSlot1 starts before timeSlot2. */ public boolean isBefore(TimeSlot timeSlot1, TimeSlot timeSlot2) { boolean before = true; System.out.println("isBefore, ts1:" + timeSlot1.getTime() + " ts2: " + timeSlot2.getTime()); if (timeSlot1.getIndex() > timeSlot2.getIndex()) { before = false; } else { before = true; } return before; } /** * Returns number of elements of TimeOrder. * * @return DOCUMENT ME! */ public int size() { return orderedTimeSlotList.size(); } /** * DOCUMENT ME! */ /* old implementation public void pruneTimeSlots() { ArrayList slotsToBeDeleted = new ArrayList(); Iterator slotIter = orderedTimeSlotList.iterator(); while (slotIter.hasNext()) { TimeSlot sl = (TimeSlot) slotIter.next(); if (((TranscriptionImpl) transcription).getAnnotationsUsingTimeSlot( sl).size() == 0) { slotsToBeDeleted.add(sl); } } //System.out.println("num of ts before: " + orderedTimeSlotList.size()); //System.out.println("num of slots to be deleted: " + slotsToBeDeleted.size()); orderedTimeSlotList.removeAll(slotsToBeDeleted); //System.out.println("num of ts after: " + orderedTimeSlotList.size()); // re-index reindex(); } */ /** * DOCUMENT ME! */ public void pruneTimeSlots() { ArrayList slotsToBeDeleted = new ArrayList(); HashSet usedSlots = ((TranscriptionImpl) transcription).getTimeSlotsInUse(); TimeSlot ts = null; for (int i = orderedTimeSlotList.size() - 1; i >= 0; i--) { ts = (TimeSlot) orderedTimeSlotList.get(i); if (!usedSlots.contains(ts)) { slotsToBeDeleted.add(ts); } } //System.out.println("num of ts before: " + orderedTimeSlotList.size()); //System.out.println("num of slots to be deleted: " + slotsToBeDeleted.size()); orderedTimeSlotList.removeAll(slotsToBeDeleted); //System.out.println("num of ts after: " + orderedTimeSlotList.size()); // re-index reindex(); } /** * DOCUMENT ME! * * @param theSlot DOCUMENT ME!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -