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

com.github.fakemongo.impl.index.IndexedList Maven / Gradle / Ivy

package com.github.fakemongo.impl.index;

import java.util.*;


public class IndexedList<Е> {
  private Map<Е, List> indexes;

  private final List<Е> elements;

  private boolean isSingle = true;

  public IndexedList(List<Е> elements) {
    this.elements = elements;

    if (elements.size() > 1) {
      initIndex(elements);
    }
  }

  private void initIndex(List<Е> newElements) {
    indexes = new HashMap<Е, List>();

    isSingle = false;

    int count = 0;
    for (Е el : newElements) {
      addIndex(el, count);
      count += 1;
    }
  }

  public List<Е> getElements() {
    return elements;
  }

  public int size() {
    return elements.size();
  }

  public boolean contains(Е element) {
    if (isSingle)
      return elements.contains(element);

    return indexes.containsKey(element);
  }

  public void add(Е element) {
    if (elements.size() == 1) {
      initIndex(elements);
    }

    elements.add(element);

    if (elements.size() > 1)
      addIndex(element, elements.size()-1);
  }

  private void addIndex(Е element, int position) {
    final List list;
    if (!indexes.containsKey(element)) {
      list = new ArrayList();
      indexes.put(element, list);
    }
    else
      list = indexes.get(element);

    list.add(position);
  }

  public void remove(Е element) {
    if (isSingle) {
      elements.remove(element);
      return;
    }

    List index = indexes.get(element);

    if (index == null)
      return;

    if (index.size() != 0) {
      int pos = index.get(0);
      elements.remove(pos);
      index.remove(0);

      for(int i=pos; i index) {
    for (int i = 0; i < index.size(); i++) {
      index.set(i, index.get(i) - 1);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy