KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > storagemodel > transientimpl > TransientObjectResolverIndex


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.mdr.storagemodel.transientimpl;
21
22 import java.lang.ref.ReferenceQueue JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import org.netbeans.mdr.persistence.*;
25 import org.netbeans.mdr.storagemodel.TransientStorableObject;
26 import org.netbeans.mdr.storagemodel.TransientStorableClass;
27 /**
28  *
29  * @author Tomas Zezula
30  */

31
32 public class TransientObjectResolverIndex extends TransactionalIndex implements SinglevaluedIndex {
33
34     public static final String JavaDoc NAME = "TRANSIENT_OBJECTS_RESOLVER";
35
36     private HashMap JavaDoc map;
37     private ReferenceQueue JavaDoc queue;
38     
39     /** Creates a new instance of ObjectResolverIndex */
40     public TransientObjectResolverIndex() {
41         this.map = new HashMap JavaDoc ();
42         this.queue = new ReferenceQueue JavaDoc ();
43     }
44     
45     public void add(Object JavaDoc key, Object JavaDoc value) throws StorageException {
46         this.addNoTx (key, value);
47         this.txlog.push (new CompensatingTransaction.AddCTx (key, value));
48     }
49     
50     public void addNoTx (Object JavaDoc key, Object JavaDoc value) throws StorageException {
51         this.map.put (key, new TransientIndex.KeyedReference (value, this.queue, (MOFID)key));
52     }
53     
54     public boolean put (Object JavaDoc key, Object JavaDoc value) throws StorageException {
55         throw new UnsupportedOperationException JavaDoc ();
56     }
57     
58     public void replace (Object JavaDoc key, Object JavaDoc value) throws StorageException {
59         throw new UnsupportedOperationException JavaDoc ();
60     }
61     
62     public Object JavaDoc get (Object JavaDoc key) throws StorageException {
63         Object JavaDoc result = this.getIfExists (key);
64         if (result == null)
65             throw new StorageBadRequestException ();
66         return result;
67     }
68     
69     public Object JavaDoc getIfExists (Object JavaDoc key) throws StorageException {
70         TransientIndex.KeyedReference entry = (TransientIndex.KeyedReference) this.map.get (key);
71         if (entry == null)
72             return null;
73         return entry.get ();
74     }
75     
76     public Object JavaDoc getObjectIfExists (Object JavaDoc key, SinglevaluedIndex index) throws StorageException {
77         TransientIndex.KeyedReference entry = (TransientIndex.KeyedReference) this.map.get (key);
78         if (entry == null)
79             return null;
80         Object JavaDoc result = entry.get ();
81         if (result == null)
82             return null;
83         return index.get (result);
84     }
85     
86     public Object JavaDoc getObject (Object JavaDoc key, SinglevaluedIndex index) throws StorageException {
87         Object JavaDoc result = this.getObjectIfExists (key, index);
88         if (result == null)
89             throw new StorageBadRequestException ();
90         return result;
91     }
92     
93
94     public Storage.EntryType getKeyType() throws StorageException {
95         return Storage.EntryType.MOFID;
96     }
97     
98     public String JavaDoc getName () throws StorageException {
99         return NAME;
100     }
101     
102     public Storage.EntryType getValueType() throws StorageException {
103         return Storage.EntryType.STREAMABLE;
104     }
105     
106     public java.util.Set JavaDoc keySet() throws StorageException {
107         throw new UnsupportedOperationException JavaDoc ();
108     }
109     
110     public java.util.Collection JavaDoc values () throws StorageException {
111         throw new UnsupportedOperationException JavaDoc ();
112     }
113     
114     public boolean remove(Object JavaDoc key) throws StorageException {
115         Object JavaDoc result = this.removeNoTx (key, null);
116         if (result != null) {
117             this.txlog.push ( new CompensatingTransaction.RemoveCTx (key, result));
118             return true;
119         }
120         else {
121             return false;
122         }
123     }
124     
125     public java.util.Collection JavaDoc queryByKeyPrefix (Object JavaDoc prefix, SinglevaluedIndex repos) {
126         throw new UnsupportedOperationException JavaDoc ();
127     }
128     
129     protected Object JavaDoc removeNoTx(Object JavaDoc key, Object JavaDoc value) throws StorageException {
130         return this.map.remove (key);
131     }
132     
133 }
134
Popular Tags