com.hortonworks.streamline.streams.layout.ConfigFieldValidation.java Source code

Java tutorial

Introduction

Here is the source code for com.hortonworks.streamline.streams.layout.ConfigFieldValidation.java

Source

/**
  * Copyright 2017 Hortonworks.
  *
  * 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.hortonworks.streamline.streams.layout;

import org.apache.commons.lang.StringUtils;

import java.util.List;

/**
 * A utility class for validating different field values entered by user
 * during topology creation. Note this class is meant for java objects
 * generated by json mapper from the json received from UI. It is based on
 * the assumption that is verified which is that mapper converts a primitive
 * type in json to either an Boolean, Integer, Long or Double depending the
 * value it represents
 */
public class ConfigFieldValidation {

    /*
    Returns true if the value can be cast to an object of class represented
    by className
     */
    public static boolean isSubType(Class clazz, Object value) {
        boolean result = false;
        if (value != null) {
            result = clazz.isAssignableFrom(value.getClass());
        }
        return result;
    }

    public static boolean isBoolean(Object value) {
        return isSubType(Boolean.class, value);
    }

    public static boolean isInteger(Object value) {
        return isSubType(Integer.class, value);
    }

    public static boolean isLong(Object value) {
        return isSubType(Long.class, value);
    }

    public static boolean isDouble(Object value) {
        return isSubType(Double.class, value);
    }

    public static boolean isString(Object value) {
        return isSubType(String.class, value);
    }

    public static boolean isList(Object value) {
        return isSubType(List.class, value);
    }

    // min and max are inclusive
    public static boolean isByteAndInRange(Object value, byte min, byte max) {
        boolean result = false;
        if (isInteger(value)) {
            Integer i = (Integer) value;
            result = isInRange(i, min, max);
        }
        return result;
    }

    // min and max are inclusive
    public static boolean isShortAndInRange(Object value, short min, short max) {
        boolean result = false;
        if (isInteger(value)) {
            Integer i = (Integer) value;
            result = isInRange(i, min, max);
        }
        return result;
    }

    // min and max are inclusive
    public static boolean isIntAndInRange(Object value, int min, int max) {
        boolean result = false;
        if (isInteger(value)) {
            Integer i = (Integer) value;
            result = isInRange(i, min, max);
        }
        return result;
    }

    // min and max are inclusive
    public static boolean isLongAndInRange(Object value, long min, long max) {
        boolean result = false;
        if (isInteger(value)) {
            Integer i = (Integer) value;
            result = isInRange(i, min, max);
        } else if (isLong(value)) {
            Long l = (Long) value;
            result = isInRange(l, min, max);
        }
        return result;
    }

    public static boolean isFloatOrDouble(Object value) {
        return isInteger(value) || isLong(value) || isDouble(value);
    }

    public static boolean isStringAndNotEmpty(Object value) {
        return isString(value) && !StringUtils.isEmpty((String) value);
    }

    public static boolean isInRange(int value, int min, int max) {
        return (value >= min && value <= max);
    }

    public static boolean isInRange(long value, long min, long max) {
        return (value >= min && value <= max);
    }

}