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

net.sf.staccatocommons.lang.function.internal.IdentityFunction Maven / Gradle / Ivy

/**
 *  Copyright (c) 2011, The Staccato-Commons Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; version 3 of the License.
 *
 *  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 Lesser General Public License for more details.
 */

package net.sf.staccatocommons.lang.function.internal;

import net.sf.staccatocommons.defs.Applicable;
import net.sf.staccatocommons.defs.Applicable2;
import net.sf.staccatocommons.defs.Thunk;
import net.sf.staccatocommons.defs.function.Function;
import net.sf.staccatocommons.defs.function.Function2;
import net.sf.staccatocommons.lang.function.Functions;
import net.sf.staccatocommons.lang.thunk.Thunks;
import net.sf.staccatocommons.restrictions.Constant;
import net.sf.staccatocommons.restrictions.check.NonNull;

/**
 * @author flbulgarelli
 * 
 * @param 
 */
public final class IdentityFunction extends TopLevelFunction {

  private static final long serialVersionUID = -9042770205177366369L;

  @Override
  public A apply(A argument) {
    return argument;
  }

  @Override
  @NonNull
  public Function nullSafe() {
    return this;
  }

  @NonNull
  @Override
  public Thunk delayed(A arg) {
    return Thunks.constant(arg);
  }

  @NonNull
  @Override
  public Thunk delayedValue(Thunk thunk) {
    return (Thunk) thunk;
  }

  @NonNull
  @Override
  public  Function2 of(Applicable2 other) {
    return Functions.from(other);
  }

  @NonNull
  @Override
  public  Function of(Applicable other) {
    return Functions.from(other);
  }

  /**
   * @param 
   * @return a constant instance
   */
  @Constant
  public static  Function identity() {
    return new IdentityFunction();
  }
  
  @Override
  public boolean isIdentity() {
    return true;
  }

}