All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.solr.cloud.autoscaling.TriggerUtils Maven / Gradle / Ivy

/*
 * 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 org.apache.solr.cloud.autoscaling;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 *
 */
public class TriggerUtils {
  // validation helper methods

  public static void requiredProperties(Set required, Set valid, String... propertyNames) {
    required.addAll(Arrays.asList(propertyNames));
    valid.addAll(Arrays.asList(propertyNames));
  }

  public static void validProperties(Set valid, String... propertyNames) {
    valid.addAll(Arrays.asList(propertyNames));
  }

  public static void checkProperties(Map properties, Map results, Set required, Set valid) {
    checkValidPropertyNames(properties, results, valid);
    checkRequiredPropertyNames(properties, results, required);
  }

  public static void checkValidPropertyNames(Map properties, Map results, Set valid) {
    Set currentNames = new HashSet<>(properties.keySet());
    currentNames.removeAll(valid);
    if (!currentNames.isEmpty()) {
      for (String name : currentNames) {
        results.put(name, "unknown property");
      }
    }
  }

  public static void checkRequiredPropertyNames(Map properties, Map results, Set required) {
    Set requiredNames = new HashSet<>(required);
    requiredNames.removeAll(properties.keySet());
    if (!requiredNames.isEmpty()) {
      for (String name : requiredNames) {
        results.put(name, "missing required property");
      }
    }
  }

  public static void checkProperty(Map properties, Map results, String name, boolean required, Class... acceptClasses) {
    Object value = properties.get(name);
    if (value == null) {
      if (required) {
        results.put(name, "missing required value");
      } else {
        return;
      }
    }
    if (acceptClasses == null || acceptClasses.length == 0) {
      return;
    }
    boolean accepted = false;
    for (Class clz : acceptClasses) {
      if (clz.isAssignableFrom(value.getClass())) {
        accepted = true;
        break;
      }
    }
    if (!accepted) {
      results.put(name, "value is not an expected type");
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy