[JAVA] Quelle zellulärer Objekte

Ein Objekt mit der Funktion zum Erstellen einer Instanz der nächsten Generation, die auf Umgebungsänderungen spezialisiert ist. Ich denke, es ist nur ein Pilotcode für Generics und Lambda.

Dies ist ein Memo.

Erstellen Sie eine Superklasse O, die bei Vererbung die entsprechende Funktion haben kann.

package premodial;

import java.util.Arrays;
import java.util.Collection;
import java.util.function.BiFunction;
import java.util.function.Function;

/**
 * Origin
 * @author dev_user
 *
 */
public abstract class O <DATA extends Object, SELF extends O<DATA, ?>>{

	/*
	 * DATA is Origin's parameter.
	 */

	private final DATA data;

	protected O(final DATA d){
		this.data = d;
	}

	public final DATA getData(){
		return data;
	}

	public abstract SELF instance(final DATA d);

	public final O<?, ?> cloneInstance(){
		return instance(data);
	}


	/*
	 * childs is Origin's childs. Childs have generation.
	 */

	protected O<?, ?> parent = null;

	protected final Collection<O<?, ?>> childs = childsCollection();

	public abstract Collection<O<?, ?>> childsCollection();

	public final void addChild(final O<?, ?> child){
		child.parent = this;
		childs.add(child);
	}

	@SafeVarargs
	public final void addChilds(final O<?, ?>...childs){
		Arrays.stream(childs).forEach(c -> addChild(c));
	}

	public final Collection<O<?, ?>> getChilds(){
		final Collection<O<?, ?>> collection = childsCollection();
		childs.stream().forEach(c -> collection.add(c));
		return collection;
	}

	public final int getChildsSize(){
		return childs.size();
	}

	@SafeVarargs
	public final void addGenerations(final O<?, ?>...generations){
		O<?, ?> o = this;
		for(O<?, ?> g : generations){
			o.addChild(g);
			o = g;
		}
	}

	public final O<?, ?> getFirstGeneration(){
		return getFirstGeneration(parent);
	}

	private static O<?, ?> getFirstGeneration(final O<?, ?> parent) {
		/*Rekursive Version
		if(parent == null){
			return null;
		}
		if(parent.parent == null){
			return parent;
		}
		return getFirstGeneration(parent.parent);
		*/
		O<?, ?> p = parent;
		while(true){
			O<?, ?> now = p;
			p = p.parent;
			if(p == null){
				return now;
			}
		}
	}

	/*
	 * Origin can create self clone, of another instance.
	 * Origin can create new self by other data in environmental.
	 * Origin can create self child(inheritance).
	 */

	public final SELF replicate(){
		final SELF o = instance(data);
		for(final O<?, ?> child : childs){
			o.addChild(child.replicate());
		}
		return o;
		/*Wenn es viele Fälle von Wiederholungen gibt, fällt diese aufgrund von Speicherüberlastung ab. Wir planen daher, sie in nicht rekursiver Form neu zu schreiben.
		SELF o = instance(data);
		Collection<O<?, ?>> childCollection = childs;
		for(final O<?, ?> child : childCollection){
			final O<?, ?> newChild = child.cloneInstance();
			o.addChild(newChild);
			if(child.childs.isEmpty()){
				continue;
			}
		}
		return o;
		*/
	}

	public final SELF evolution(final Function<DATA, DATA> environmental){
		return instance(environmental.apply(data));
	}

	@SuppressWarnings("unchecked")
	public final SELF evolution(final BiFunction<DATA, DATA, DATA> environmental){
		return instance(environmental.apply(parent != null ? (DATA)parent.data : null, data));
	}

	public final SELF inheritance(){
		final SELF child = instance(data);
		addChild(child);
		return child;
	}

	public final SELF inheritance(final Function<DATA, DATA> environmental){
		final SELF child = evolution(environmental);
		addChild(child);
		return child;
	}

	public final SELF inheritance(final BiFunction<DATA, DATA, DATA> environmental){
		final SELF child = evolution(environmental);
		addChild(child);
		return child;
	}

	public final void print(){
		System.out.println(data.toString() + ", data_hashCode=" + data.hashCode() + ", o_hashCode=" + hashCode() + ", childs=" + getChildsSize());
	}

	protected Function<DATA, DATA> environmentalMemory = null;

	public final void memoryEnvironmental(final Function<DATA, DATA> environmentalMemory){
		this.environmentalMemory = environmentalMemory;
	}

	public final SELF nextGeneration(){
		final SELF g = instance(environmentalMemory != null ? environmentalMemory.apply(data) : data);
		g.environmentalMemory = environmentalMemory;
		addChild(g);
		return g;
	}

}

Ein Objekt, das lange verarbeitet und O erbt.

package premodial;

import java.util.ArrayList;
import java.util.Collection;

public final class OLong extends O<Long, OLong>{

	protected OLong(Long d){
		super(d);
	}

	@Override
	public OLong instance(Long d){
		return of(d);
	}

	public static OLong of(Long i){
		return new OLong(i);
	}

	@Override
	public Collection<O<?, ?>> childsCollection(){
		return new ArrayList<O<?, ?>>();
	}

}

Verwenden Sie OLong, um die Fibonacci-Sequenz zu berechnen.

		System.out.println("fibonacci------------");
		final BiFunction<Long, Long, Long> fibonacciCalc = (p, c) -> {
			if(p == null){
				return 0l;
			}else if(p == 0){
				return 1l;
			}else{
				return p + c;
			}
		};
		long start = System.currentTimeMillis();
		OLong fibonacci = OLong.of(0l);
		OLong fibonacciFirst = fibonacci;
		int fibonacciLoop = 20;
		for(int i = 0; i < fibonacciLoop; i++){
			fibonacci = fibonacci.inheritance(fibonacciCalc);
			System.out.println(fibonacci.getData().toString() + ", o_HashCode=" + fibonacci.hashCode());
		}
		System.out.println((System.currentTimeMillis() - start) + "ms");

		System.out.println("fibonacci(simple)----");
		start = System.currentTimeMillis();
		long a = 0;
		System.out.println(a);
		long b = 1;
		System.out.println(b);
		for(int i = 0; i < (fibonacciLoop - 2); i++){
			long c = a + b;
			System.out.println(c);
			a = b;
			b = c;
		}
		System.out.println((System.currentTimeMillis() - start) + "ms");

		System.out.println("fibonacci childs-----");
		printChilds(fibonacciFirst, true);
		start = System.currentTimeMillis();
		OLong fibonacciReplicated = fibonacciFirst.replicate();
		System.out.println((System.currentTimeMillis() - start) + "ms");
		printChilds(fibonacciReplicated, true);

Ausführungsergebnis.

fibonacci------------
0, o_HashCode=1190900417
1, o_HashCode=379110473
1, o_HashCode=99550389
2, o_HashCode=1598924227
3, o_HashCode=1144748369
5, o_HashCode=340870931
8, o_HashCode=1768305536
13, o_HashCode=1530388690
21, o_HashCode=1146743572
34, o_HashCode=1512981843
55, o_HashCode=42768293
89, o_HashCode=1717159510
144, o_HashCode=1834188994
233, o_HashCode=1174361318
377, o_HashCode=589873731
610, o_HashCode=200006406
987, o_HashCode=2052001577
1597, o_HashCode=1160264930
2584, o_HashCode=544724190
4181, o_HashCode=1972439101
4ms
fibonacci(simple)----
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
1ms
fibonacci childs-----
0, data_hashCode=0, o_hashCode=2007328737, childs=1
0, data_hashCode=0, o_hashCode=1190900417, childs=1
1, data_hashCode=1, o_hashCode=379110473, childs=1
1, data_hashCode=1, o_hashCode=99550389, childs=1
2, data_hashCode=2, o_hashCode=1598924227, childs=1
3, data_hashCode=3, o_hashCode=1144748369, childs=1
5, data_hashCode=5, o_hashCode=340870931, childs=1
8, data_hashCode=8, o_hashCode=1768305536, childs=1
13, data_hashCode=13, o_hashCode=1530388690, childs=1
21, data_hashCode=21, o_hashCode=1146743572, childs=1
34, data_hashCode=34, o_hashCode=1512981843, childs=1
55, data_hashCode=55, o_hashCode=42768293, childs=1
89, data_hashCode=89, o_hashCode=1717159510, childs=1
144, data_hashCode=144, o_hashCode=1834188994, childs=1
233, data_hashCode=233, o_hashCode=1174361318, childs=1
377, data_hashCode=377, o_hashCode=589873731, childs=1
610, data_hashCode=610, o_hashCode=200006406, childs=1
987, data_hashCode=987, o_hashCode=2052001577, childs=1
1597, data_hashCode=1597, o_hashCode=1160264930, childs=1
2584, data_hashCode=2584, o_hashCode=544724190, childs=1
4181, data_hashCode=4181, o_hashCode=1972439101, childs=0
0ms
0, data_hashCode=0, o_hashCode=1936628443, childs=1
0, data_hashCode=0, o_hashCode=1830908236, childs=1
1, data_hashCode=1, o_hashCode=277630005, childs=1
1, data_hashCode=1, o_hashCode=1288354730, childs=1
2, data_hashCode=2, o_hashCode=1274370218, childs=1
3, data_hashCode=3, o_hashCode=758705033, childs=1
5, data_hashCode=5, o_hashCode=1604839423, childs=1
8, data_hashCode=8, o_hashCode=1177096266, childs=1
13, data_hashCode=13, o_hashCode=670576685, childs=1
21, data_hashCode=21, o_hashCode=1299641336, childs=1
34, data_hashCode=34, o_hashCode=764308918, childs=1
55, data_hashCode=55, o_hashCode=598446861, childs=1
89, data_hashCode=89, o_hashCode=1161082381, childs=1
144, data_hashCode=144, o_hashCode=1067938912, childs=1
233, data_hashCode=233, o_hashCode=1637506559, childs=1
377, data_hashCode=377, o_hashCode=517380410, childs=1
610, data_hashCode=610, o_hashCode=2117255219, childs=1
987, data_hashCode=987, o_hashCode=2058534881, childs=1
1597, data_hashCode=1597, o_hashCode=1232367853, childs=1
2584, data_hashCode=2584, o_hashCode=1673605040, childs=1
4181, data_hashCode=4181, o_hashCode=186276003, childs=0

Recommended Posts

Quelle zellulärer Objekte
Ich habe die Quelle von ArrayList gelesen, die ich gelesen habe
Liste der Java-Objekte sortieren
Ich habe die Quelle von Integer gelesen
Ich habe die Quelle von Long gelesen
Ich habe die Quelle von Short gelesen
Ich habe die Quelle von Byte gelesen
Ich habe die Quelle von String gelesen
Grundstruktur des Java-Quellcodes
Erläuterung der Ruby Time- und Date-Objekte
Messen Sie einfach die Größe von Java-Objekten
Vergleich von JavaScript-Objekten und Ruby-Klassen