alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.mdr.persistence.memoryimpl;

import org.netbeans.mdr.persistence.*;
import java.util.ArrayList;
import java.util.List;

/** Default memory implementation of the MultivaluedOrderedIndex using Hashtable.
 * @author  Pavel Buzek, Martin Matula
 * @version
 */
public class MultivaluedOrderedIndexImpl extends MultivaluedIndexImpl implements MultivaluedOrderedIndex, Streamable {
    
    /** Creates new MultivaluedOrderedIndexImpl */
    public MultivaluedOrderedIndexImpl(String name, StorageImpl storage, Storage.EntryType keyType, Storage.EntryType valueType, boolean unique) {
        super (name, storage, keyType, valueType, unique);
    }

    public MultivaluedOrderedIndexImpl() {
    }

    public synchronized java.util.List getItemsOrdered(Object key) throws StorageException {
        return (List) getItems (key);
    }

      /** Like getItemsOrdered, but if the index contains keys, this returns the objects 
     * corresponding to the key
     * @return
     * @param key
     * @throws StorageException
     */
    public synchronized java.util.Collection getObjectsOrdered (Object key, SinglevaluedIndex repos) throws StorageException {
    	return (List) getObjects(key, repos);
    }

    /** Removes the element at the specified position in the list of values
     * associated with the specified key.
     */
    public synchronized boolean remove(Object key,int index) throws StorageException {
        List vals = (List) entries.get (key);
        Object orig;
        if (vals != null && (orig = vals.remove(index)) != null) {
            transLog.logRemove(key, orig, index);
            return true;
        } else {
            return false;
        }
    }
    
    /** Inserts the specified element at the specified position in the list of values
     * associated with the specified key.
     */
    public synchronized void add(Object key,int index,Object value) throws StorageException {
        List vals = (List) entries.get (key);
        if (vals == null) {
            vals = new ArrayList ();
            entries.put(key, vals);
        } else if (unique && vals.contains(value)) {
            throw createValueAlreadyContainedExc(key, value);
        }
        vals.add(index, value);
        transLog.logAdd(key, value, index);
    }
    
    /** Replaces the element at the specified position in the list of values
     * associated with the specified key with the specified element.
     * Throws StorageBadRequestException if the index is out of range.
     * @param key
     * @param index
     * @param element
     * @throws StorageException
     */
    public synchronized void replace(Object key,int index,Object element) throws StorageException {
        List vals = (List) entries.get (key);
        try {
            Object orig;
            if (vals != null) {
                if (unique && !isUniqueValue(vals, element, index)) {
                    throw createValueAlreadyContainedExc(key, element);
                }
                if ((orig = vals.set(index, element)) != null) {
                    transLog.logReplace(key, orig, index);
                    return;
                }
            }
        } catch (IndexOutOfBoundsException e) {
        }
        throw new StorageBadRequestException ("Index out of range: " + index + " for key: " + key);
    }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.