org.opensearch.client.PreferHasAttributeNodeSelector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opensearch-rest-client Show documentation
Show all versions of opensearch-rest-client Show documentation
OpenSearch subproject :client:rest
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch 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.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
package org.opensearch.client;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* Both {@link PreferHasAttributeNodeSelector} and {@link HasAttributeNodeSelector} will work the same
* if there is a {@link Node} with particular attribute in the attributes,
* but {@link PreferHasAttributeNodeSelector} will select another {@link Node}s even if there is no {@link Node}
* with particular attribute in the attributes.
*/
public final class PreferHasAttributeNodeSelector implements NodeSelector {
private final String key;
private final String value;
/**
* Creates a {@link PreferHasAttributeNodeSelector} instance with the given key value pair.
*
* @param key attribute key
* @param value attribute value
*/
public PreferHasAttributeNodeSelector(String key, String value) {
this.key = key;
this.value = value;
}
@Override
public void select(Iterable nodes) {
boolean foundAtLeastOne = false;
for (Node node : nodes) {
Map> attributes = node.getAttributes();
if (attributes == null) {
continue;
}
List values = attributes.get(key);
if (values == null) {
continue;
}
if (values.contains(value)) {
foundAtLeastOne = true;
break;
}
}
if (foundAtLeastOne) {
Iterator nodeIterator = nodes.iterator();
while (nodeIterator.hasNext()) {
Map> attributes = nodeIterator.next().getAttributes();
if (attributes == null) {
continue;
}
List values = attributes.get(key);
if (values == null || !values.contains(value)) {
nodeIterator.remove();
}
}
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
PreferHasAttributeNodeSelector that = (PreferHasAttributeNodeSelector) o;
return Objects.equals(key, that.key) && Objects.equals(value, that.value);
}
@Override
public int hashCode() {
return Objects.hash(key, value);
}
@Override
public String toString() {
return key + "=" + value;
}
}