/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ Franz Wilhelmstötter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author:
 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
 */
package org.jenetics.util;

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 * @version @__version__@ &mdash; <em>$Date: 2014-01-30 $</em>
 * @since @__version__@
 */
public class RandRegistry {

	private static final ThreadLocal<Entry> ENTRY = new ThreadLocal<>();

	private static AtomicReference<Entry> entry = new AtomicReference<>(
		new Entry(ThreadLocalRandom.current())
	);

	static Random getRandom() {
		return getEntry().random;
	}

	static void setRandom(final Random random) {
		setEntry(random);
	}

	private static Entry getEntry() {
		final Entry e = ENTRY.get();
		return e != null ? e : entry.get();
	}

	private static void setEntry(final Random random) {
		final Entry e = ENTRY.get();
		if (e != null) e.random = random; else entry.set(new Entry(random));
	}

	static Context<Random> with(final Random random) {
		final Entry e = ENTRY.get();
		if (e != null) {
			ENTRY.set(e.inner(random));
		} else {
			ENTRY.set(new Entry(random));
		}

		return new Ctx(random);
	}

	private static final class Ctx implements Context<Random> {
		final Random _random;

		public Ctx(Random random) {
			_random = random;
		}

		@Override
		public Random get() {
			return _random;
		}

		@Override
		public void close() {
			ENTRY.set(ENTRY.get().parent);
		}
	}

	private static final class Entry {
		Random random;
		Entry parent;

		Entry(final Random random, final Entry parent) {
			this.random = random;
			this.parent = parent;
		}

		Entry(final Random random) {
			this(random, null);
		}

		Entry inner(final Random random) {
			return new Entry(random, this);
		}
	}

}

@@ -1847,24 +1847,48 @@
 Entry(random
+, Thread.currentThread()
 ));%0A%09%09%7D%0A%0A%09%09r
@@ -1973,24 +1973,32 @@
 Random%3E %7B%0A%09%09
+private 
 final Random
@@ -2021,16 +2021,22 @@
 lic Ctx(
+final 
 Random r
@@ -2173,29 +2173,233 @@
 %0A%09%09%09
-ENTRY.set(
+final Entry e = ENTRY.get();%0A%09%09%09if (e != null) %7B%0A%09%09%09%09if (e.thread != Thread.currentThread()) %7B%0A%09%09%09%09%09throw new IllegalStateException(%0A%09%09%09%09%09%09%22Random context must be closed by the creating thread.%22%0A%09%09%09%09%09);%0A%09%09%09%09%7D%0A%0A%09%09%09%09
 ENTRY.
-get()
+set(e
 .par
@@ -2410,81 +2410,218 @@
 ;%0A%09%09
-%7D%0A%09%7D%0A%0A%09private static final class Entry %7B%0A%09%09Random random;%0A%09%09Entry parent
+%09%7D else %7B%0A%09%09%09%09throw new IllegalStateException(%0A%09%09%09%09%09%22Random context has been already close.%22%0A%09%09%09%09);%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A%09private static final class Entry %7B%0A%09%09final Thread thread;%0A%09%09final Entry parent;%0A%0A%09%09Random random
 ;%0A%0A%09
@@ -2666,16 +2666,37 @@
 y parent
+, final Thread thread
 ) %7B%0A%09%09%09t
@@ -2741,16 +2741,216 @@
 parent;%0A
+%09%09%09this.thread = thread;%0A%09%09%7D%0A%0A%09%09Entry(final Random random, final Entry parent) %7B%0A%09%09%09this(random, parent, null);%0A%09%09%7D%0A%0A%09%09Entry(final Random random, final Thread thread) %7B%0A%09%09%09this(random, null, thread);%0A
 %09%09%7D%0A%0A%09%09E
@@ -2988,24 +2988,30 @@
 this(random,
+ null,
  null);%0A%09%09%7D%0A
@@ -3044,24 +3044,69 @@
 m random) %7B%0A
+%09%09%09assert(thread == Thread.currentThread());%0A
 %09%09%09return ne
@@ -3125,16 +3125,24 @@
 om, this
+, thread
 );%0A%09%09%7D%0A%09

Implement new random context.