All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nuiton.math.matrix.SemanticList Maven / Gradle / Ivy

The newest version!
/* *##% NuitonMatrix
 * Copyright (C) 2004 - 2009 CodeLutin
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 *
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * . ##%*/

package org.nuiton.math.matrix;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.RandomAccess;

/**
 * SemanticList.
 *
 * Created: 6 sept. 06 17:18:23
 *
 * @param  elements type
 * 
 * @author poussin
 * @version $Revision: 187 $
 *
 * Last update: $Date: 2009-10-16 19:17:29 +0200 (ven., 16 oct. 2009) $
 * by : $Author: tchemit $
 */
public class SemanticList extends AbstractList implements RandomAccess {

    protected ArrayList datas = null;
    protected Map index = null;

    public SemanticList(Collection c) {
        datas = new ArrayList(c);
    }

    /*
     * @see java.util.AbstractList#get(int)
     */
    @Override
    public T get(int index) {
        T result = datas.get(index);
        return result;
    }

    /*
     * @see java.util.AbstractCollection#size()
     */
    @Override
    public int size() {
        int result = datas.size();
        return result;
    }

    /*
     * @see java.util.AbstractList#indexOf(java.lang.Object)
     */
    @Override
    public int indexOf(Object o) {
        Map i = getIndex();
        Integer result = i.get(o);
        int resultIndex = -1;
        if (result != null) {
            resultIndex = result.intValue();
        }
        return resultIndex;
    }

    protected Map getIndex() {
        if (index == null) {
            index = new HashMap();
            for (int i = 0; i < datas.size(); i++) {
                index.put(datas.get(i), Integer.valueOf(i));
            }
        }
        return index;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy