Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*******************************************************************************
* Copyright 2022 EPAM Systems
*
* 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.epam.eco.kafkamanager.ui.utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import org.apache.commons.lang3.Range;
import org.apache.commons.lang3.Validate;
import com.epam.eco.kafkamanager.ui.utils.CollapsedCollectionIterable.Element;
/**
* @author Andrei_Tytsik
*/
public class CollapsedCollectionIterable implements Iterable> {
private static final int AFTER_DELIMITER_SIZE = 1;
private static final Element> DELIMITER = new Element<>();
public static class Element {
private final T value;
private final boolean divider;
public Element() {
this(null, true);
}
public Element(T value) {
this(value, false);
}
private Element(T value, boolean divider) {
this.value = value;
this.divider = divider;
}
public T getValue() {
return value;
}
public boolean isDivider() {
return divider;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
@Override
public boolean equals(Object obj) {
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}
if (this == obj) {
return true;
}
Element> that = (Element>)obj;
return Objects.equals(this.value, that.value);
}
@Override
public String toString() {
return Objects.toString(value, "...");
}
}
private final List> collapsedCollection;
public CollapsedCollectionIterable(Collection collection, int collapsedSize) {
Validate.notNull(collection, "Collection is null");
this.collapsedCollection = collapseCollection(collection, null, collapsedSize);
}
public CollapsedCollectionIterable(Collection collection, Function mapper, int collapsedSize) {
Validate.notNull(collection, "Collection is null");
this.collapsedCollection = collapseCollection(collection, mapper, collapsedSize);
}
@Override
public Iterator> iterator() {
return collapsedCollection.iterator();
}
@SuppressWarnings("unchecked")
private List> collapseCollection(Collection> collection, Function, ?> mapper, int collapsedSize) {
collapsedSize = adjustCollapsedSize(collapsedSize, collection.size());
int delimiterIndex = getDelimiterIndex(collapsedSize, collection.size());
Range rangeBeforeDelimiter = getRangeBeforeDelimiter(delimiterIndex, collapsedSize);
Range rangeAfterDelimiter = getRangeAfterDelimiter(delimiterIndex, collection.size());
List> collapsed = new ArrayList<>();
int idx = 0;
for (Object value : collection) {
if (rangeBeforeDelimiter.contains(idx) || rangeAfterDelimiter.contains(idx)) {
collapsed.add(asElement(value, (Function