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

selva.oss.lang.Commons Maven / Gradle / Ivy

The newest version!
package selva.oss.lang;

import java.util.*;
import java.util.stream.Stream;

public class Commons {

    public static class Nothing {

        public static Optional of(boolean bool) {
            return bool ? Optional.of(new Nothing()) : Optional.empty();
        }

    }

    public interface Equable {
        public boolean isEqual(T value);
    }

    public interface Hashable {
        public Stream getSimplifiedDataView();
    }

    public static  boolean checkIfEqual(Class type, Equable thisValue, Optional newValue) {
        if (!newValue.isPresent()) {
            return false;
        }
        if (thisValue == newValue.get()) {
            return true;
        }
        if (!type.isInstance(newValue.get())) {
            return false;
        }
        return thisValue.isEqual((T) newValue.get());
    }

    public static int produceHash(Hashable hashable) {
        return Objects.hash(hashable.getSimplifiedDataView().toArray());
    }

    public static class CommonValidationFailedException extends RuntimeException {

        public enum CommonValidationFailedState {
            NullNotExpected, UnexpectedType, CaseNotHandled
        }

        private CommonValidationFailedState commonValidationFailedState;

        public CommonValidationFailedException(CommonValidationFailedState commonValidationFailedState) {
            super(commonValidationFailedState.toString());
            this.commonValidationFailedState = commonValidationFailedState;
        }

        public CommonValidationFailedException(CommonValidationFailedState commonValidationFailedState, String message) {
            super(message);
            this.commonValidationFailedState = commonValidationFailedState;
        }

        public CommonValidationFailedState getCommonValidationFailedState() {
            return this.commonValidationFailedState;
        }

    }

    public static void validateNotNull(Object value) {
        if (value == null) {
            throw new CommonValidationFailedException(CommonValidationFailedException.CommonValidationFailedState.NullNotExpected);
        }
    }

    public static void validateType(Class valueType, Object value) {
        validateNotNull(valueType);

        if (!valueType.isInstance(value)) {
            throw new CommonValidationFailedException(CommonValidationFailedException.CommonValidationFailedState.UnexpectedType);
        }
    }

    public static > CommonValidationFailedException throwCaseNotHandledException(T notHandledCase) {
        validateNotNull(notHandledCase);

        return new CommonValidationFailedException(CommonValidationFailedException.CommonValidationFailedState.CaseNotHandled,
                "Case " + notHandledCase.toString() + " is not handled.");
    }

    public static class HasNotBeenImplementedException extends RuntimeException {
    }

}