Aklakan commented on code in PR #2405: URL: https://github.com/apache/jena/pull/2405#discussion_r1724456633
########## jena-arq/src/main/java/org/apache/jena/sparql/engine/join/ImmutableUniqueList.java: ########## @@ -0,0 +1,208 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.jena.sparql.engine.join; + +import java.lang.reflect.Array; +import java.util.AbstractList; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.lang3.ArrayUtils; + +/** + * An immutable list without duplicates. + * + * For lists of size {@value ImmutableUniqueList#INDEX_THRESHOLD} or larger, + * the {@link #indexOf(Object)} method will on first use index the elements for faster access. + */ +public class ImmutableUniqueList<T> extends AbstractList<T> { + /** Threshold in the number of variables for when to use additional indexing structures + * in order to improve scalability */ + private static final int INDEX_THRESHOLD = 5; + + /** The builder can emit a key every time build() is called + * and it can be continued to be used. + */ + public static final class Builder<T> { + private Class<T> itemClass; + + /** + * The keys collection upgrades itself from ArrayList to + * LinkedHashSet upon adding a sufficient number of items. + */ + private Collection<T> items; + + Builder(Class<T> itemClass) { + super(); + this.itemClass = itemClass; + } + + private void alloc(int n) { + if (items == null) { + items = n < INDEX_THRESHOLD ? new ArrayList<>(INDEX_THRESHOLD) : new LinkedHashSet<>(); + } else if (!(items instanceof Set) && (items.size() + n >= INDEX_THRESHOLD)) { + Set<T> tmp = new LinkedHashSet<>(items); + items = tmp; + } + } + + public Builder<T> add(T item) { + if (!(items instanceof Set)) { + if ( items == null || ! items.contains(item) ) { + alloc(1); + items.add(item) ; + } + } else { + items.add(item); + } + return this ; + } + + public Builder<T> addAll(Collection<T> items) { + alloc(items.size()); + for (T item : items) { + add(item); + } + return this; + } + + public Builder<T> addAll(T[] arr) { + alloc(arr.length); + for (T item : arr) { + add(item); + } + return this; + } + + public Builder<T> remove(Object o) { + if (items != null) { + items.remove(o) ; + } + return this ; + } + + public Builder<T> clear() { + items = null; + return this ; + } + + public boolean isEmpty() { + return items == null || items.isEmpty(); + } + + @SuppressWarnings("unchecked") + public ImmutableUniqueList<T> build() { + T[] finalItems; + if (items == null) { + finalItems = (T[])Array.newInstance(itemClass, 0); + } else { + finalItems = (T[])Array.newInstance(itemClass, items.size()); + items.toArray(finalItems); + } + return new ImmutableUniqueList<>(INDEX_THRESHOLD, finalItems); + } + } + + public static <T> Builder<T> newUniqueListBuilder(Class<T> itemClass) { + return new Builder<>(itemClass); + } + + public static <T> ImmutableUniqueList<T> createUniqueList(Class<T> itemClass, Collection<T> items) { + return ImmutableUniqueList.<T>newUniqueListBuilder(itemClass).addAll(items).build(); + } + + public static <T> ImmutableUniqueList<T> createUniqueList(Class<T> itemClass, T[] items) { + return ImmutableUniqueList.<T>newUniqueListBuilder(itemClass).addAll(items).build(); + } + + /** + * Create a join key without coping the key array and without checking for duplicates. + * The array must not be modified. + */ +// @SuppressWarnings("unchecked") +// public static <T> ImmutableUniqueList<T> createUniqueListUnsafe(int indexThreshold, T[] elementData) { Review Comment: Yes, should be removed. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: pr-unsubscr...@jena.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: pr-unsubscr...@jena.apache.org For additional commands, e-mail: pr-h...@jena.apache.org