
io.rxmicro.common.model.ListBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxmicro-common Show documentation
Show all versions of rxmicro-common Show documentation
The common module with base models and useful utils.
/*
* Copyright (c) 2020. https://rxmicro.io
*
* 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 io.rxmicro.common.model;
import io.rxmicro.common.InvalidStateException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import static io.rxmicro.common.util.ExCollections.unmodifiableList;
/**
* The builder that builds a short-lived unmodified {@link List} instance.
*
* @author nedis
* @param the type of elements in this list
* @see List
* @see ArrayList
* @see io.rxmicro.common.util.ExCollections#unmodifiableList(Collection)
* @since 0.1
*/
public class ListBuilder {
private final List list = new ArrayList<>();
private boolean built;
/**
* Adds the specified value to the building {@link List} instance.
*
* @param value the specified value
* @return the reference to this {@link ListBuilder} instance
* @throws InvalidStateException if the {@link List} instance already built
*/
public ListBuilder add(final E value) {
if (built) {
throw new InvalidStateException("This builder can't be used, because the list instance already built! Create a new builder!");
}
list.add(value);
return this;
}
/**
* Builds the short-lived unmodified {@link List} instance.
*
* @return the short-lived unmodified {@link List} instance
*/
public List build() {
built = true;
return unmodifiableList(list);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy