com.datastax.oss.driver.internal.querybuilder.ImmutableCollections Maven / Gradle / Ivy
The newest version!
/*
* 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 com.datastax.oss.driver.internal.querybuilder;
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList;
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Map;
import java.util.function.Function;
public class ImmutableCollections {
@NonNull
public static ImmutableList append(@NonNull ImmutableList list, @NonNull T newElement) {
return ImmutableList.builder().addAll(list).add(newElement).build();
}
@NonNull
public static ImmutableList concat(
@NonNull ImmutableList list1, @NonNull Iterable list2) {
return ImmutableList.builder().addAll(list1).addAll(list2).build();
}
@NonNull
public static ImmutableList modifyLast(
@NonNull ImmutableList list, @NonNull Function change) {
ImmutableList.Builder builder = ImmutableList.builder();
int size = list.size();
for (int i = 0; i < size - 1; i++) {
builder.add(list.get(i));
}
builder.add(change.apply(list.get(size - 1)));
return builder.build();
}
/**
* If the existing map has an entry with the new key, that old entry will be removed, but the new
* entry will appear last in the iteration order of the resulting map. Example:
*
* {@code
* append({a=>1, b=>2, c=>3}, a, 4) == {b=>2, c=>3, a=>4}
* }
*/
@NonNull
public static ImmutableMap append(
@NonNull ImmutableMap map, @NonNull K newKey, @NonNull V newValue) {
ImmutableMap.Builder builder = ImmutableMap.builder();
for (Map.Entry entry : map.entrySet()) {
if (!entry.getKey().equals(newKey)) {
builder.put(entry);
}
}
builder.put(newKey, newValue);
return builder.build();
}
/**
* If the existing map has entries that collide with the new map, those old entries will be
* removed, but the new entries will appear at their new position in the iteration order of the
* resulting map. Example:
*
* {@code
* concat({a=>1, b=>2, c=>3}, {c=>4, a=>5}) == {b=>2, c=>4, a=>5}
* }
*/
@NonNull
public static ImmutableMap concat(
@NonNull ImmutableMap map1, @NonNull Map map2) {
ImmutableMap.Builder builder = ImmutableMap.builder();
for (Map.Entry entry : map1.entrySet()) {
if (!map2.containsKey(entry.getKey())) {
builder.put(entry);
}
}
builder.putAll(map2);
return builder.build();
}
}