/***************************************************************************
 *   Copyright (C) by Fabrizio Montesi                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 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 Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 *                                                                         *
 *   For details about the authors of this software, see the AUTHORS file. *
 ***************************************************************************/

package jolie;

import java.util.HashMap;
import java.util.Map;
import jolie.runtime.InternalLink;
import jolie.runtime.Value;

/**
 * A variable and links state.
 * Variables are stored in a tree-like structure, of which the root Value element is stored.
 * @see Value
 * @author Fabrizio Montesi
 */
public final class State implements Cloneable
{
	private final Value root;
	private final Map< String, InternalLink > linksMap = new HashMap<>();
	
	private State( Value root )
	{
		this.root = root;
	}
	
	/**
	 * Returns the InternalLink identified by id in this State scope.
	 * @param id the identifier of the requested InternalLink
	 * @return the InternalLink identified by id
	 */
	public InternalLink getLink( String id )
	{
		InternalLink l = linksMap.get( id );
		if ( l == null ) {
			l = new InternalLink( id );
			linksMap.put( id, l );
		}
		return l;
	}
	
	/**
	 * Constructs a new State, using a fresh memory state.
	 */
	public State()
	{
		this.root = Value.createRootValue();
	}
	
	@Override
	public State clone()
	{
		return new State( Value.createClone( root ) );
	}
	
	/**
	 * Returns the root Value of this State.
	 * @return the root Value of this State
	 * @see Value
	 */
	public Value root()
	{
		return root;
	}
}

@@ -1572,34 +1572,33 @@
 til.
-HashMap;%0Aimport java.util.
+concurrent.ConcurrentHash
 Map;
@@ -1926,16 +1926,30 @@
 e final 
+ConcurrentHash
 Map%3C Str
@@ -1983,16 +1983,26 @@
 p = new 
+Concurrent
 HashMap%3C
@@ -2298,137 +2298,73 @@
 %7B%0A%09%09
-InternalLink l = linksMap.get( id );%0A%09%09if ( l == null ) %7B%0A%09%09%09l = new InternalLink( id );%0A%09%09%09linksMap.put( id, l );%0A%09%09%7D%0A%09%09return l
+return linksMap.computeIfAbsent( id, k -%3E new InternalLink( k ) )
 ;%0A%09%7D

Fix a race condition on internal links.