mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
introduce OpenTHashSet & fix OrderedSet
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2000-2011 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.util.containers;
|
||||
|
||||
import gnu.trove.THashSet;
|
||||
import gnu.trove.TObjectHashingStrategy;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* @author gregsh
|
||||
*/
|
||||
public class OpenTHashSet<T> extends THashSet<T> {
|
||||
public OpenTHashSet() {
|
||||
super();
|
||||
}
|
||||
|
||||
public OpenTHashSet(final TObjectHashingStrategy<T> strategy) {
|
||||
super(strategy);
|
||||
}
|
||||
|
||||
public OpenTHashSet(final int initialCapacity) {
|
||||
super(initialCapacity);
|
||||
}
|
||||
|
||||
public OpenTHashSet(final int initialCapacity, final TObjectHashingStrategy<T> strategy) {
|
||||
super(initialCapacity, strategy);
|
||||
}
|
||||
|
||||
public OpenTHashSet(final int initialCapacity, final float loadFactor) {
|
||||
super(initialCapacity, loadFactor);
|
||||
}
|
||||
|
||||
public OpenTHashSet(final int initialCapacity, final float loadFactor, final TObjectHashingStrategy<T> strategy) {
|
||||
super(initialCapacity, loadFactor, strategy);
|
||||
}
|
||||
|
||||
public OpenTHashSet(final Collection<? extends T> ts) {
|
||||
super(ts);
|
||||
}
|
||||
|
||||
public OpenTHashSet(final Collection<? extends T> ts, final TObjectHashingStrategy<T> strategy) {
|
||||
super(ts, strategy);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int index(final T obj) {
|
||||
return super.index(obj);
|
||||
}
|
||||
|
||||
public T get(final int index) {
|
||||
return (T)_set[index];
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public T get(final T obj) {
|
||||
final int index = index(obj);
|
||||
return index < 0 ? null : get(index);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,34 +15,29 @@
|
||||
*/
|
||||
package com.intellij.util.containers;
|
||||
|
||||
import gnu.trove.THashSet;
|
||||
import gnu.trove.TObjectHashingStrategy;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class OrderedSet<T> extends AbstractSet<T> implements List<T> {
|
||||
private final Set<T> myHashSet;
|
||||
private final List<T> myElements;
|
||||
public class OrderedSet<T> extends AbstractList<T> implements Set<T>, RandomAccess {
|
||||
private final OpenTHashSet<T> myHashSet;
|
||||
private final ArrayList<T> myElements;
|
||||
|
||||
public OrderedSet(TObjectHashingStrategy<T> hashingStrategy) {
|
||||
myHashSet = new THashSet<T>(hashingStrategy);
|
||||
myHashSet = new OpenTHashSet<T>(hashingStrategy);
|
||||
myElements = new ArrayList<T>();
|
||||
}
|
||||
|
||||
public OrderedSet(TObjectHashingStrategy<T> hashingStrategy, int capacity) {
|
||||
myHashSet = new THashSet<T>(capacity, hashingStrategy);
|
||||
myHashSet = new OpenTHashSet<T>(capacity, hashingStrategy);
|
||||
myElements = new ArrayList<T>(capacity);
|
||||
}
|
||||
|
||||
public OrderedSet() {
|
||||
myHashSet = new THashSet<T>();
|
||||
myHashSet = new OpenTHashSet<T>();
|
||||
myElements = new ArrayList<T>();
|
||||
}
|
||||
|
||||
public Iterator<T> iterator() {
|
||||
return new MyIterator();
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return myElements.size();
|
||||
}
|
||||
@@ -93,25 +88,6 @@ public class OrderedSet<T> extends AbstractSet<T> implements List<T> {
|
||||
}
|
||||
}
|
||||
|
||||
private class MyIterator implements Iterator<T> {
|
||||
private final Iterator<T> myIterator = myElements.iterator();
|
||||
private T myLastObject;
|
||||
|
||||
public boolean hasNext() {
|
||||
return myIterator.hasNext();
|
||||
}
|
||||
|
||||
public T next() {
|
||||
return myLastObject = myIterator.next();
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
myIterator.remove();
|
||||
myHashSet.remove(myLastObject);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean addAll(final int index, final Collection<? extends T> c) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
@@ -139,11 +115,13 @@ public class OrderedSet<T> extends AbstractSet<T> implements List<T> {
|
||||
}
|
||||
|
||||
public int indexOf(final Object o) {
|
||||
return myElements.indexOf(o);
|
||||
final int index = myHashSet.index((T)o);
|
||||
return index >= 0? myElements.indexOf(myHashSet.get(index)) : -1;
|
||||
}
|
||||
|
||||
public int lastIndexOf(final Object o) {
|
||||
return myElements.lastIndexOf(o);
|
||||
final int index = myHashSet.index((T)o);
|
||||
return index >= 0 ? myElements.lastIndexOf(myHashSet.get(index)) : -1;
|
||||
}
|
||||
|
||||
public ListIterator<T> listIterator() {
|
||||
@@ -153,8 +131,4 @@ public class OrderedSet<T> extends AbstractSet<T> implements List<T> {
|
||||
public ListIterator<T> listIterator(final int index) {
|
||||
return myElements.listIterator(index);
|
||||
}
|
||||
|
||||
public List<T> subList(final int fromIndex, final int toIndex) {
|
||||
return myElements.subList(fromIndex, toIndex);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user