com.landawn.abacus.util.NoCachingNoUpdating Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-common Show documentation
Show all versions of abacus-common Show documentation
A general programming library in Java/Android. It's easy to learn and simple to use with concise and powerful APIs.
The newest version!
/*
* Copyright (C) 2019 HaiYang Li
*
* 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.landawn.abacus.util;
import java.util.AbstractMap;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Deque;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.IntFunction;
import com.landawn.abacus.annotation.Beta;
import com.landawn.abacus.annotation.SequentialOnly;
import com.landawn.abacus.annotation.Stateful;
import com.landawn.abacus.annotation.SuppressFBWarnings;
/**
* One-off Object. No caching/saving in memory, No updating. To cache/save/update the Object, call {@code copy()}.
*
*
*/
@Beta
@SequentialOnly
@Stateful
public interface NoCachingNoUpdating {
/**
* One-off Object. No caching/saving in memory, No updating. To cache/save/update the Object, call {@code copy()}.
*
* Depends on context, it should be okay to cache/save the elements from the array, but never save or cache the array itself.
*
* @param
*/
@Beta
@SequentialOnly
@Stateful
@SuppressFBWarnings("CN_IDIOM_NO_SUPER_CALL")
class DisposableArray implements NoCachingNoUpdating, Iterable {
/** The element array */
private final T[] a;
/**
* Instantiates a new disposable array.
*
* @param a
*/
protected DisposableArray(final T[] a) {
N.checkArgNotNull(a, cs.a);
this.a = a;
}
/**
*
* @param
* @param componentType
* @param len
* @return
*/
public static DisposableArray create(final Class componentType, final int len) {
return new DisposableArray<>(N.newArray(componentType, len));
}
/**
*
* @param
* @param a
* @return
*/
public static DisposableArray wrap(final T[] a) {
return new DisposableArray<>(a);
}
/**
*
* @param index
* @return
*/
public T get(final int index) {
return a[index];
}
public int length() {
return a.length;
}
/**
*
* @param
* @param target
* @return
*/
public A[] toArray(A[] target) {
if (target.length < length()) {
target = N.newArray(target.getClass().getComponentType(), length());
}
N.copy(a, 0, target, 0, length());
return target;
}
public T[] copy() { //NOSONAR
return N.clone(a);
}
public List toList() {
return N.toList(a);
}
public Set toSet() {
return N.toSet(a);
}
/**
*
* @param
* @param supplier
* @return
*/
public > C toCollection(final IntFunction extends C> supplier) {
final C result = supplier.apply(length());
result.addAll(toList());
return result;
}
/**
*
* @param
* @param action
* @throws E
*/
public void forEach(final Throwables.Consumer super T, E> action) throws E {
for (final T e : a) {
action.accept(e);
}
}
/**
*
* @param
* @param
* @param func
* @return
* @throws E
*/
public R apply(final Throwables.Function super T[], ? extends R, E> func) throws E {
return func.apply(a);
}
/**
*
* @param
* @param action
* @throws E
*/
public void accept(final Throwables.Consumer super T[], E> action) throws E {
action.accept(a);
}
/**
*
* @param delimiter
* @return
*/
public String join(final String delimiter) {
return Strings.join(a, delimiter);
}
/**
*
* @param delimiter
* @param prefix
* @param suffix
* @return
*/
public String join(final String delimiter, final String prefix, final String suffix) {
return Strings.join(a, delimiter, prefix, suffix);
}
@Override
public Iterator iterator() {
return ObjIterator.of(a);
}
@Override
public String toString() {
return N.toString(a);
}
protected T[] values() {
return a;
}
}
/**
* One-off Object. No caching/saving in memory, No updating. To cache/save/update the Object, call {@code copy()}.
*
* Depends on context, it should be okay to cache/save the elements from the array, but never save or cache the array itself.
*/
@Beta
@SequentialOnly
@Stateful
class DisposableObjArray extends DisposableArray
© 2015 - 2025 Weber Informatics LLC | Privacy Policy