View Javadoc

1   /*
2    * Copyright 2004 Charles Blaxland
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package mnemosyne.core;
17  
18  import mnemosyne.util.PersistenceRuntimeException;
19  
20  import java.io.Serializable;
21  import java.util.ArrayList;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.ListIterator;
25  
26  /***
27   * An implementation of VersionCollection based on a simple ArrayList.
28   *
29   * @version $Id: ListVersionCollection.java,v 1.1.1.1 2004/08/07 06:41:02 charlesblaxland Exp $
30   */
31  public class ListVersionCollection implements VersionCollection
32  {
33      private List versions = new ArrayList(4);
34  
35      public Object retrieveVersion(Version versionToGet)
36      {
37          VersionedObject obj = null;
38          ListIterator lIter = versions.listIterator(versions.size());
39          while (lIter.hasPrevious())
40          {
41              obj = (VersionedObject)lIter.previous();
42              int compareResult = obj.version.compareTo(versionToGet);
43              if (compareResult <= 0)
44              {
45                  return obj.object;
46              }
47          }
48  
49          // There should always be a version available so if we get here its an error
50          throw new PersistenceRuntimeException("No value found for version " + versionToGet);
51      }
52  
53      public void replaceVersion(Version version, Object obj)
54      {
55          for (Iterator iterator = versions.iterator(); iterator.hasNext();)
56          {
57              VersionedObject versionedObject = (VersionedObject) iterator.next();
58              if (versionedObject.version.equals(version))
59              {
60                  versionedObject.object = obj;
61                  return;
62              }
63          }
64  
65          // Error condition if the requested version is not found
66          throw new PersistenceRuntimeException("No existing object found for replaceVersion");
67      }
68  
69      public void addVersion(Version version, Object obj)
70      {
71          if (!versions.isEmpty())
72          {
73              VersionedObject lastObj = (VersionedObject)versions.get(versions.size() - 1);
74              int compareResult = version.compareTo(lastObj.version);
75              if (compareResult <= 0)
76              {
77                  throw new PersistenceRuntimeException("Attempted to add a version earlier than the latest existing version");
78              }
79          }
80  
81          versions.add(new VersionedObject(version, obj));
82      }
83  
84      public void removeVersion(Version version)
85      {
86          VersionedObject obj = null;
87          ListIterator lIter = versions.listIterator(versions.size());
88          while (lIter.hasPrevious())
89          {
90              obj = (VersionedObject)lIter.previous();
91              int compareResult = obj.version.compareTo(version);
92              if (compareResult == 0)
93              {
94                  lIter.remove();
95                  return;
96              }
97          }
98      }
99  
100     public int size()
101     {
102         return versions.size();
103     }
104 
105     protected class VersionedObject implements Serializable
106     {
107         public Version version;
108         public Object object;
109 
110         public VersionedObject(Version version, Object object)
111         {
112             this.version = version;
113             this.object = object;
114         }
115     }
116 }