/*******************************************************************************
 * Copyright (c) 2016 IBH SYSTEMS GmbH and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBH SYSTEMS GmbH - initial API and implementation
 *******************************************************************************/
package org.eclipse.packagedrone.utils;

import java.util.function.Function;

/**
 * Map only once
 * <p>
 * This class helps to map from a source value to a target value where many
 * mapping variants are possible. After the first successful mapping (the
 * mapping result is not {@code null}) further mapping calls will be ignored.
 * </p>
 *
 * @param <T>
 *            the type of the input type
 * @param <R>
 *            the type of the result type
 */
public class MapOnce<T, R>
{
    private final T value;

    private R result;

    public MapOnce ( final T value )
    {
        this.value = value;
    }

    public void map ( final Function<T, R> func )
    {
        if ( this.result == null )
        {
            this.result = func.apply ( this.value );
        }
    }

    /**
     * Get the mapping result
     *
     * @return the mapping result, may be {@code null}
     */
    public R get ()
    {
        return this.result;
    }
}

@@ -571,16 +571,43 @@
 utils;%0A%0A
+import java.util.Optional;%0A
 import j
@@ -1090,16 +1090,46 @@
 ate 
-R result
+Optional%3CR%3E result = Optional.empty ()
 ;%0A%0A 
@@ -1245,17 +1245,27 @@
 tion%3CT, 
-R
+Optional%3CR%3E
 %3E func )
@@ -1284,16 +1284,17 @@
    if ( 
+!
 this.res
@@ -1300,16 +1300,21 @@
 sult
- == null
+.isPresent ()
  )%0A 
@@ -1479,16 +1479,69 @@
  may be 
+%7B@link Optional#empty()%7D but is never%0A     *         
 %7B@code n
@@ -1564,17 +1564,27 @@
  public 
-R
+Optional%3CR%3E
  get ()%0A

implement using Optional