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

com.strobel.core.Selectors Maven / Gradle / Ivy

/*
 * Selectors.java
 *
 * Copyright (c) 2012 Mike Strobel
 *
 * This source code is subject to terms and conditions of the Apache License, Version 2.0.
 * A copy of the license can be found in the License.html file at the root of this distribution.
 * By using this source code in any fashion, you are agreeing to be bound by the terms of the
 * Apache License, Version 2.0.
 *
 * You must not remove this notice, or any other, from this software.
 */

package com.strobel.core;

import com.strobel.util.ContractUtils;

/**
 * @author Mike Strobel
 */
@SuppressWarnings("unchecked")
public final class Selectors {
    private final static Selector IDENTITY_SELECTOR = new Selector() {
        @Override
        public Object select(final Object source) {
            return source;
        }
    };

    private final static Selector TO_UPPERCASE = new Selector() {
        @Override
        public String select(final String source) {
            if (source == null) {
                return null;
            }
            return source.toUpperCase();
        }
    };

    private final static Selector TO_LOWERCASE = new Selector() {
        @Override
        public String select(final String source) {
            if (source == null) {
                return null;
            }
            return source.toUpperCase();
        }
    };

    private final static Selector TO_STRING = new Selector() {
        @Override
        public String select(final Object source) {
            if (source == null) {
                return null;
            }
            return source.toString();
        }
    };

    private Selectors() {
        throw ContractUtils.unreachable();
    }

    public static  Selector identity() {
        return (Selector)IDENTITY_SELECTOR;
    }

    public static Selector toUpperCase() {
        return TO_UPPERCASE;
    }

    public static Selector toLowerCase() {
        return TO_LOWERCASE;
    }

    public static  Selector asString() {
        return (Selector)TO_STRING;
    }

    public static  Selector cast(final Class destinationType) {
        return new Selector() {
            @Override
            public R select(final T source) {
                return destinationType.cast(source);
            }
        };
    }

    public static  Selector combine(
        final Selector first,
        final Selector second) {

        VerifyArgument.notNull(first, "first");
        VerifyArgument.notNull(second, "second");

        return new Selector() {
            @Override
            public R select(final T source) {
                return second.select(first.select(source));
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy