KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > persistence > memoryimpl > MultivaluedOrderedIndexImpl


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 package org.netbeans.mdr.persistence.memoryimpl;
20
21 import org.netbeans.mdr.persistence.*;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 /** Default memory implementation of the MultivaluedOrderedIndex using Hashtable.
26  * @author Pavel Buzek, Martin Matula
27  * @version
28  */

29 public class MultivaluedOrderedIndexImpl extends MultivaluedIndexImpl implements MultivaluedOrderedIndex, Streamable {
30
31     /** Creates new MultivaluedOrderedIndexImpl */
32     public MultivaluedOrderedIndexImpl(String JavaDoc name, StorageImpl storage, Storage.EntryType keyType, Storage.EntryType valueType, boolean unique) {
33         super (name, storage, keyType, valueType, unique);
34     }
35
36     public MultivaluedOrderedIndexImpl() {
37     }
38
39     public synchronized java.util.List JavaDoc getItemsOrdered(Object JavaDoc key) throws StorageException {
40         return (List JavaDoc) getItems (key);
41     }
42
43       /** Like getItemsOrdered, but if the index contains keys, this returns the objects
44      * corresponding to the key
45      * @return
46      * @param key
47      * @throws StorageException
48      */

49     public synchronized java.util.Collection JavaDoc getObjectsOrdered (Object JavaDoc key, SinglevaluedIndex repos) throws StorageException {
50         return (List JavaDoc) getObjects(key, repos);
51     }
52
53     /** Removes the element at the specified position in the list of values
54      * associated with the specified key.
55      */

56     public synchronized boolean remove(Object JavaDoc key,int index) throws StorageException {
57         List JavaDoc vals = (List JavaDoc) entries.get (key);
58         Object JavaDoc orig;
59         if (vals != null && (orig = vals.remove(index)) != null) {
60             transLog.logRemove(key, orig, index);
61             return true;
62         } else {
63             return false;
64         }
65     }
66     
67     /** Inserts the specified element at the specified position in the list of values
68      * associated with the specified key.
69      */

70     public synchronized void add(Object JavaDoc key,int index,Object JavaDoc value) throws StorageException {
71         List JavaDoc vals = (List JavaDoc) entries.get (key);
72         if (vals == null) {
73             vals = new ArrayList JavaDoc ();
74             entries.put(key, vals);
75         } else if (unique && vals.contains(value)) {
76             throw createValueAlreadyContainedExc(key, value);
77         }
78         vals.add(index, value);
79         transLog.logAdd(key, value, index);
80     }
81     
82     /** Replaces the element at the specified position in the list of values
83      * associated with the specified key with the specified element.
84      * Throws StorageBadRequestException if the index is out of range.
85      * @param key
86      * @param index
87      * @param element
88      * @throws StorageException
89      */

90     public synchronized void replace(Object JavaDoc key,int index,Object JavaDoc element) throws StorageException {
91         List JavaDoc vals = (List JavaDoc) entries.get (key);
92         try {
93             Object JavaDoc orig;
94             if (vals != null) {
95                 if (unique && !isUniqueValue(vals, element, index)) {
96                     throw createValueAlreadyContainedExc(key, element);
97                 }
98                 if ((orig = vals.set(index, element)) != null) {
99                     transLog.logReplace(key, orig, index);
100                     return;
101                 }
102             }
103         } catch (IndexOutOfBoundsException JavaDoc e) {
104         }
105         throw new StorageBadRequestException ("Index out of range: " + index + " for key: " + key);
106     }
107 }
108
Popular Tags