View Javadoc

1   /*
2    * Copyright (c) 2005 Regents of the University of California (Regents). Created
3    * by TELS, Graduate School of Education, University of California at Berkeley.
4    *
5    * This software is distributed under the GNU Lesser General Public License, v2.
6    *
7    * Permission is hereby granted, without written agreement and without license
8    * or royalty fees, to use, copy, modify, and distribute this software and its
9    * documentation for any purpose, provided that the above copyright notice and
10   * the following two paragraphs appear in all copies of this software.
11   *
12   * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
13   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
14   * PURPOSE. THE SOFTWAREAND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
15   * HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
16   * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17   *
18   * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
19   * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
20   * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
21   * REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22   */
23  package net.sf.sail.common.persistance.memory;
24  
25  import java.util.Date;
26  import java.util.Iterator;
27  import java.util.Stack;
28  
29  import net.sf.sail.core.entity.ISock;
30  import net.sf.sail.core.entity.ISockEntry;
31  import net.sf.sail.core.entity.Rim;
32  
33  public class MemorySock<T> extends Stack<T> implements ISock<T> {
34  
35  	/**
36  	 * @author turadg
37  	 * 
38  	 */
39  	public final class MemorySockEntry implements ISockEntry<T> {
40  
41  		public Date getDate() {
42  			throw new UnsupportedOperationException(
43  					"date not yet implemented on MemorySockEntry");
44  		}
45  
46  		public T getValue() {
47  			return peek();
48  		}
49  
50  	}
51  
52  	/**
53  	 * 
54  	 */
55  	private static final long serialVersionUID = -752831757729119985L;
56  
57  	transient static int sockCount = 0;
58  
59  	transient int sockSerial = ++sockCount;
60  
61  	private Rim<T> rim;
62  
63  	/**
64  	 * @param rim
65  	 */
66  	public MemorySock(Rim<T> rim) {
67  		this.rim = rim;
68  	}
69  
70  	public synchronized boolean add(T o, boolean replace) {
71  		return add(o);
72  	}
73  	
74  	@Override
75  	public synchronized boolean add(T o) {
76  		final Class<?> clazz = o.getClass();
77  		final Class<T> shape = rim.getShape();
78  		if (!shape.isAssignableFrom(clazz))
79  			throw new ClassCastException("sock " + this + " is shaped " + shape);
80  		return super.add(o);
81  	}
82  
83  	@Override
84  	public synchronized String toString() {
85  		return "ISock@" + sockSerial + super.toString();
86  	}
87  
88  	/**
89  	 * FIXME this isn't really implemented
90  	 */
91  	public Iterator<ISockEntry<T>> entryIterator() {
92  		return new Iterator<ISockEntry<T>>() {
93  
94  			public boolean hasNext() {
95  				return false;
96  			}
97  
98  			public ISockEntry<T> next() {
99  				return null;
100 			}
101 
102 			public void remove() {
103 				throw new UnsupportedOperationException(
104 						"no remove on this iterator");
105 			}
106 
107 		};
108 	}
109 
110 	public ISockEntry<T> entryPeek() {
111 		return new MemorySockEntry();
112 	}
113 
114 }