/*
 * Copyright 2014 Takao Nakaguchi
 *
 * 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.
 */
package org.trie4j;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.junit.Test;
import org.trie4j.doublearray.TailDoubleArray;
import org.trie4j.louds.TailLOUDSTrie;
import org.trie4j.patricia.TailPatriciaTrie;
import org.trie4j.tail.DefaultTailArray;
import org.trie4j.test.LapTimer;
import org.trie4j.test.WikipediaTitles;

public abstract class AbstractWikipediaSerializeTest{
	protected Trie firstTrie(){
		return new TailPatriciaTrie();
	}
	protected Trie secondTrie(Trie first){
		return first;
	}

	@Test
	public void test() throws Exception{
		WikipediaTitles wt = new WikipediaTitles();
		Trie trie = wt.insertTo(firstTrie());
		trie = secondTrie(trie);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(baos);
		LapTimer lt = new LapTimer();
		oos.writeObject(trie);
		oos.flush();
		long wd = lt.lapMillis();
		byte[] serialized = baos.toByteArray();
		lt.reset();
		Trie t = (Trie)new ObjectInputStream(new ByteArrayInputStream(serialized))
				.readObject();
		long rd = lt.lapMillis();
		long vd = wt.assertAllContains(t);
		System.out.println(String.format(
				"%s%s%s, size: %d, write(ms): %d, read(ms): %d, verify(ms): %d.",
				trie.getClass().getSimpleName(),
				getBvTreeClassName(trie),
				getTailClassName(trie),
				serialized.length, wd, rd, vd
				));
	}

	static String getBvTreeClassName(Trie trie){
		if(trie instanceof TailLOUDSTrie){
			return "(" + ((TailLOUDSTrie)trie).getBvTree().getClass().getSimpleName() + ")";
		} else{
			return "";
		}
	}

	static String getTailClassName(Trie trie){
		if(trie instanceof TailPatriciaTrie){
			return "(" + ((TailPatriciaTrie) trie).getTailBuilder().getClass().getSimpleName() + ")";
		} else if(trie instanceof TailDoubleArray){
			return "(" + ((DefaultTailArray)((TailDoubleArray)trie).getTailArray()).getTailIndex().getClass().getSimpleName() + ")";
		} else if(trie instanceof TailLOUDSTrie){
			return "(" + ((DefaultTailArray)((TailLOUDSTrie)trie).getTailArray()).getTailIndex().getClass().getSimpleName() + ")";
		} else{
			return "";
		}
	}
}

@@ -859,24 +859,153 @@
 lLOUDSTrie;%0A
+import org.trie4j.louds.bvtree.BvTree;%0Aimport org.trie4j.louds.bvtree.LOUDSBvTree;%0Aimport org.trie4j.louds.bvtree.LOUDSPPBvTree;%0A
 import org.t
@@ -2252,36 +2252,84 @@
 DSTrie)%7B%0A%09%09%09
-return %22(%22 +
+StringBuilder b = new StringBuilder(%22(%22);%0A%09%09%09BvTree bvTree =
  ((TailLOUDS
@@ -2350,16 +2350,36 @@
 BvTree()
+;%0A%09%09%09b.append(bvTree
 .getClas
@@ -2389,38 +2389,511 @@
 .getSimpleName()
- + %22)%22
+);%0A%09%09%09if(bvTree instanceof LOUDSBvTree)%7B%0A%09%09%09%09b.append(%22(%22);%0A%09%09%09%09b.append(((LOUDSBvTree)bvTree).getSbv().getClass().getSimpleName());%0A%09%09%09%09b.append(%22)%22);%0A%09%09%09%7D else if(bvTree instanceof LOUDSPPBvTree)%7B%0A%09%09%09%09b.append(%22(%22);%0A%09%09%09%09LOUDSPPBvTree pbvt = (LOUDSPPBvTree)bvTree;%0A%09%09%09%09b.append(%22r0:%22)%0A%09%09%09%09%09.append(pbvt.getR0().getClass().getSimpleName())%0A%09%09%09%09%09.append(%22,r1:%22)%0A%09%09%09%09%09.append(pbvt.getR1().getClass().getSimpleName())%0A%09%09%09%09%09.append(%22)%22);%0A%09%09%09%7D%0A%09%09%09b.append(%22)%22);%0A%09%09%09return b.toString()
 ;%0A%09%09%7D else%7B%0A%09%09%09r

add codes for LOUDSPPBvTree.