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

org.neo4j.helpers.Functions Maven / Gradle / Ivy

/**
 * Copyright (c) 2002-2013 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package org.neo4j.helpers;

import java.util.Map;

public final class Functions
{
    public static  Function map( final Map map )
    {
        return new Function()
        {
            @Override
            public To apply( From from )
            {
                return map.get( from );
            }
        };
    }

    public static  Function withDefaults( final Function defaults, final Function f )
    {
        return new Function()
        {
            @Override
            public To apply( From from )
            {
                To to = f.apply( from );

                if ( to == null )
                {
                    return defaults.apply( from );
                }
                else
                {
                    return to;
                }
            }
        };
    }

    public static  Function nullFunction()
    {
        return new Function()
        {
            @Override
            public To apply( From from )
            {
                return null; // Always return null
            }
        };
    }

    public static  Function constant( final To value )
    {
        return new Function()
        {
            @Override
            public To apply( From from )
            {
                return value;
            }
        };
    }

    public static  Function2, Function, Function> compose()
    {
        return new Function2, Function, Function>()
        {
            @Override
            public Function apply( final Function f1, final Function f2 )
            {
                return new Function()
                {
                    @Override
                    public To apply( From from )
                    {
                        return f2.apply( f1.apply( from ) );
                    }
                };
            }
        };
    }

    public static  Function2, Function2, Function2> compose2()
    {
        return new Function2, Function2, Function2>()
        {
            @Override
            public Function2 apply( final Function2 function1, final Function2 function2 )
            {
                return new Function2()
                {
                    @Override
                    public T1 apply( T1 from1, T2 from2 )
                    {
                        return function1.apply( function2.apply( from1, from2 ), from2 );
                    }
                };
            }
        };
    }

    public static Function TO_STRING = new Function()
    {
        @Override
        public String apply( Object from )
        {
            if (from != null)
                return from.toString();
            else
                return "";
        }
    };

    private Functions()
    {
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy