![JAR search and dependency download from the Maven repository](/logo.png)
com.streamsets.pipeline.api.base.BaseEnumChooserValues Maven / Gradle / Ivy
/*
* Copyright 2017 StreamSets Inc.
*
* 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.streamsets.pipeline.api.base;
import com.streamsets.pipeline.api.ChooserValues;
import com.streamsets.pipeline.api.Label;
import com.streamsets.pipeline.api.impl.Utils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Base {@link ChooserValues} implementation for enums.
*/
public abstract class BaseEnumChooserValues implements ChooserValues {
private String resourceBundle;
private List values;
private List labels;
/**
* Creates a ChooserValues
with all the enum values of the given enum class.
*
* @param klass enum class.
*/
@SuppressWarnings("unchecked")
public BaseEnumChooserValues(Class extends Enum> klass) {
this((T[])klass.getEnumConstants());
}
/**
* Creates a ChooserValues
with the specified enum values.
*
* @param enums enums for the ChooserValues
.
*/
@SuppressWarnings("unchecked")
public BaseEnumChooserValues(T ... enums) {
Utils.checkNotNull(enums, "enums");
Utils.checkArgument(enums.length > 0, "array enum cannot have zero elements");
resourceBundle = enums[0].getClass().getName() + "-bundle";
boolean isEnumWithLabels = enums[0] instanceof Label;
values = new ArrayList<>(enums.length);
labels = new ArrayList<>(enums.length);
for (T e : enums) {
String value = e.name();
values.add(value);
String label = isEnumWithLabels ? ((Label)e).getLabel() : value;
labels.add(label);
}
values = Collections.unmodifiableList(values);
labels = Collections.unmodifiableList(labels);
}
@Override
public String getResourceBundle() {
return resourceBundle;
}
@Override
public List getValues() {
return values;
}
@Override
public List getLabels() {
return labels;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy