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.
/*
* 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.List;
import java.util.Map;
import java.util.Set;
import com.landawn.abacus.annotation.Beta;
import com.landawn.abacus.annotation.SequentialOnly;
import com.landawn.abacus.annotation.Stateful;
import com.landawn.abacus.annotation.SuppressFBWarnings;
import com.landawn.abacus.util.function.IntFunction;
/**
* One-off Object. No caching/saving in memory, No updating. To cache/save/update the Object, call {@code clone()/copy()}.
*
* @since 1.3
*
* @author Haiyang Li
*/
@Beta
@SequentialOnly
@Stateful
public interface NoCachingNoUpdating {
/**
* One-off Object. No caching/saving in memory, No updating. To cache/save/update the Object, call {@code clone()/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")
public static class DisposableArray implements NoCachingNoUpdating, Cloneable {
/** The a. */
private final T[] a;
/**
* Instantiates a new disposable array.
*
* @param a
*/
protected DisposableArray(final T[] a) {
N.checkArgNotNull(a, "a");
this.a = a;
}
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];
}
/**
*
* @return
*/
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(this.a, 0, target, 0, length());
return target;
}
/**
*
* @return
*/
@Override
public T[] clone() {
return N.clone(a);
}
/**
*
* @return
*/
public List toList() {
return N.toList(clone());
}
/**
*
* @return
*/
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 action
*/
public void forEach(final Throwables.Consumer super T, E> action) throws E {
for (T e : a) {
action.accept(e);
}
}
/**
*
* @param
* @param func
* @return
*/
public R apply(final Throwables.Function super T[], R, E> func) throws E {
return func.apply(a);
}
/**
*
* @param action
*/
public void accept(final Throwables.Consumer super T[], E> action) throws E {
action.accept(a);
}
/**
*
* @param delimiter
* @return
*/
public String join(String delimiter) {
return StringUtil.join(a, delimiter);
}
/**
*
* @param delimiter
* @param prefix
* @param suffix
* @return
*/
public String join(String delimiter, String prefix, String suffix) {
return StringUtil.join(a, delimiter, prefix, suffix);
}
/**
*
* @return
*/
@Override
public String toString() {
return N.toString(a);
}
/**
*
* @return
*/
protected T[] values() {
return a;
}
}
/**
* One-off Object. No caching/saving in memory, No updating. To cache/save/update the Object, call {@code clone()/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
public static class DisposableObjArray extends DisposableArray