org.elasticsearch.xpack.core.security.authz.ResolvedIndices Maven / Gradle / Ivy
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
package org.elasticsearch.xpack.core.security.authz;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.elasticsearch.xpack.core.security.authz.IndicesAndAliasesResolverField.NO_INDEX_PLACEHOLDER;
/**
* Stores a collection of index names separated into "local" and "remote".
* This allows the resolution and categorization to take place exactly once per-request.
*/
public final class ResolvedIndices {
private final List local;
private final List remote;
public ResolvedIndices(List local, List remote) {
this.local = Collections.unmodifiableList(local);
this.remote = Collections.unmodifiableList(remote);
}
/**
* Returns the collection of index names that have been stored as "local" indices.
* This is a List
because order may be important. For example [ "a*" , "-a1" ]
is interpreted differently
* to [ "-a1", "a*" ]
. As a consequence, this list may contain duplicates.
*/
public List getLocal() {
return local;
}
/**
* Returns the collection of index names that have been stored as "remote" indices.
*/
public List getRemote() {
return remote;
}
/**
* @return true
if both the {@link #getLocal() local} and {@link #getRemote() remote} index lists are empty.
*/
public boolean isEmpty() {
return local.isEmpty() && remote.isEmpty();
}
/**
* @return true
if the {@link #getRemote() remote} index lists is empty, and the local index list contains the
* {@link IndicesAndAliasesResolverField#NO_INDEX_PLACEHOLDER no-index-placeholder} and nothing else.
*/
public boolean isNoIndicesPlaceholder() {
return remote.isEmpty() && local.size() == 1 && local.contains(NO_INDEX_PLACEHOLDER);
}
public String[] toArray() {
final String[] array = new String[local.size() + remote.size()];
int i = 0;
for (String index : local) {
array[i++] = index;
}
for (String index : remote) {
array[i++] = index;
}
return array;
}
/**
* Builder class for ResolvedIndices that allows for the building of a list of indices
* without the need to construct new objects and merging them together
*/
public static class Builder {
private final List local = new ArrayList<>();
private final List remote = new ArrayList<>();
/** add a local index name */
public void addLocal(String index) {
local.add(index);
}
/** adds the array of local index names */
public void addLocal(String[] indices) {
local.addAll(Arrays.asList(indices));
}
/** adds the list of local index names */
public void addLocal(List indices) {
local.addAll(indices);
}
/** adds the list of remote index names */
public void addRemote(List indices) {
remote.addAll(indices);
}
/** @return true
if both the local and remote index lists are empty. */
public boolean isEmpty() {
return local.isEmpty() && remote.isEmpty();
}
/** @return a immutable ResolvedIndices instance with the local and remote index lists */
public ResolvedIndices build() {
return new ResolvedIndices(local, remote);
}
}
}