All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mentalog.LogEvent Maven / Gradle / Ivy

There is a newer version: 2.1.2
Show newest version
package org.mentalog;

import org.mentalog.util.StringBuilderUtils;

public class LogEvent {

	private enum Type {
		LONG, DOUBLE, CHAR, BOOLEAN, STRING_BUILDER, OBJECT
	}

	private static final int MAX = 32;
	private static final int STRING_BUILDER_SIZE = 512;

	private final long[] longs = new long[MAX];
	private int longPointer = -1;

	private final double[] doubles = new double[MAX];
	private int doublePointer = -1;

	private final char[] chars = new char[MAX];
	private int charPointer = -1;

	private final boolean[] bools = new boolean[MAX];
	private int boolPointer = -1;

	private final StringBuilder[] sbs = new StringBuilder[MAX];
	private int sbPointer = -1;

	private final Object[] objects = new Object[MAX];
	private int objectPointer = -1;

	private final Type[] order = new Type[MAX * 3];
	private int typePointer = -1;
	
	private int iterationPointer = -1;
	
	private Logger logger;

	public LogEvent() {
		for (int i = 0; i < MAX; i++) {
			sbs[i] = new StringBuilder(STRING_BUILDER_SIZE);
		}
	}
	
	public final void setLogger(Logger logger) {
		this.logger = logger;
	}
	
	public final int size() {
		return typePointer + 1;
	}

	public void flip() {
		iterationPointer = -1;
		longPointer = 0;
		doublePointer = 0;
		charPointer = 0;
		boolPointer = 0;
		sbPointer = 0;
		objectPointer = 0;
	}

	public void reset() {
		iterationPointer = -1;
		typePointer = -1;
		longPointer = -1;
		doublePointer = -1;
		charPointer = -1;
		boolPointer = -1;
		sbPointer = -1;
		objectPointer = -1;
	}

	public boolean hasNext() {
		return iterationPointer < typePointer;
	}

	public Object next() {
		Type type = order[++iterationPointer];
		switch (type) {
		case BOOLEAN:
			return StringBuilderUtils.to_sb(bools[boolPointer++]);
		case DOUBLE:
			return StringBuilderUtils.to_sb(doubles[doublePointer++]);
		case LONG:
			return StringBuilderUtils.to_sb(longs[longPointer++]);
		case CHAR:
			return StringBuilderUtils.to_sb(chars[charPointer++]);
		case STRING_BUILDER:
			return sbs[sbPointer++];
		case OBJECT:
			return objects[objectPointer++];
		default:
			throw new IllegalStateException("Does not recognize this type: " + type);
		}
	}

	// LONG, DOUBLE, CHAR, BOOLEAN, BYTE_ARRAY, CHAR_ARRAY, STRING_BUILDER, OBJECT

	public void addCopy(AsciiEncodable cs) {
		order[++typePointer] = Type.STRING_BUILDER;
		StringBuilder sb = sbs[++sbPointer];
		sb.setLength(0);
		cs.toAscii(sb);
	}


	public void addCopy(char[] x) {
		order[++typePointer] = Type.STRING_BUILDER;
		StringBuilder sb = sbs[++sbPointer];
		sb.setLength(0);
		for (int i = 0; i < x.length; i++) {
			sb.append(x[i]);
		}
	}


	public void addCopy(byte[] x) {
		order[++typePointer] = Type.STRING_BUILDER;
		StringBuilder sb = sbs[++sbPointer];
		sb.setLength(0);
		for (int i = 0; i < x.length; i++) {
			sb.append((char) x[i]);
		}
	}
	
	public void addCopy(StringBuilder stringBuilder) {
		order[++typePointer] = Type.STRING_BUILDER;
		StringBuilder sb = sbs[++sbPointer];
		sb.setLength(0);
		sb.append(stringBuilder);
	}
	
	public void addCopy(Object o) {
		// this better be immutable...
		add(o);
	}
	
	public void add(long x) {
		order[++typePointer] = Type.LONG;
		longs[++longPointer] = x;
	}


	public void add(int x) {
		add((long) x);
	}

	public void add(double x) {
		order[++typePointer] = Type.DOUBLE;
		doubles[++doublePointer] = x;
	}


	public void add(float x) {
		add((double) x);
	}


	public void add(byte x) {
		add((long) x);
	}


	public void add(short x) {
		add((long) x);
	}


	public void add(char x) {
		order[++typePointer] = Type.CHAR;
		chars[++charPointer] = x;
	}


	public void add(boolean x) {
		order[++typePointer] = Type.BOOLEAN;
		bools[++boolPointer] = x;
	}

	public void add(Object o) {
		order[++typePointer] = Type.OBJECT;
		objects[++objectPointer] = o;
	}
	
	final void log() {
		int size = size();
		Object[] varargs = logger.getVarargs(size);
		flip();
		for(int i = 0; i < size; i++) {
			varargs[i] = next();
		}
		logger.log(false, varargs);
		reset();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy