Ver Fonte

Moving format code to submodule

Sam Jaffe há 8 anos atrás
pai
commit
e628e2aff9

+ 3 - 0
.gitmodules

@@ -7,3 +7,6 @@
 [submodule "include/observer"]
 	path = include/observer
 	url = http://209.6.89.209:3000/samjaffe/java-util-observer.git
+[submodule "include/format"]
+	path = include/format
+	url = http://209.6.89.209:3000/samjaffe/java-util-format.git

+ 1 - 0
include/format

@@ -0,0 +1 @@
+Subproject commit 5efe6472cf59d3036611c74b90ed1cf636b44433

+ 1 - 0
include/pom.xml

@@ -18,6 +18,7 @@
     <module>event</module>
     <module>graphics</module>
     <module>observer</module>
+    <module>format</module>
     <module>..</module>
   </modules>
 </project>

+ 5 - 0
pom.xml

@@ -173,6 +173,11 @@
       <artifactId>observer</artifactId>
       <version>0.1</version>
     </dependency>
+    <dependency>
+      <groupId>org.leumasjaffe</groupId>
+      <artifactId>format</artifactId>
+      <version>0.1</version>
+    </dependency>
   </dependencies>
   <parent>
     <groupId>org.leumasjaffe</groupId>

+ 0 - 18
src/main/lombok/org/leumasjaffe/format/CustomToString.java

@@ -1,18 +0,0 @@
-package org.leumasjaffe.format;
-
-import java.util.function.Function;
-
-import lombok.AccessLevel;
-import lombok.AllArgsConstructor;
-import lombok.experimental.FieldDefaults;
-
-@AllArgsConstructor
-@FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
-public class CustomToString<T> {
-	T object;
-	Function<T, String> toStringFunction;
-	
-	public String toString() {
-		return toStringFunction.apply(object);
-	}
-}

+ 0 - 27
src/main/lombok/org/leumasjaffe/format/Named.java

@@ -1,27 +0,0 @@
-package org.leumasjaffe.format;
-
-import java.util.Objects;
-
-import lombok.AccessLevel;
-import lombok.Data;
-import lombok.experimental.FieldDefaults;
-
-@Data
-@FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
-public class Named {
-	String name;
-	Object value;
-	
-	public Named(String n, Object v) {
-		Objects.requireNonNull(n);
-		this.name = n;
-		this.value = v;
-		if (n.matches("\\{|\\}|%")) {
-			throw new IllegalArgumentException("Cannot use '{', '}', or '%' in named arguments!");
-		}
-	}
-	
-	public String toString() {
-		return String.valueOf(value);
-	}
-}

+ 0 - 145
src/main/lombok/org/leumasjaffe/format/StringFormatter.java

@@ -1,145 +0,0 @@
-package org.leumasjaffe.format;
-
-import java.util.HashMap;
-import java.util.Objects;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import lombok.AccessLevel;
-import lombok.experimental.FieldDefaults;
-
-@FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
-public class StringFormatter {
-	private static final class NameMap extends HashMap<String, Object> {
-		/**
-		 * 
-		 */
-		private static final long serialVersionUID = -2243574068000774442L;
-
-		NameMap(Object... args) {
-			for (Object arg : args) {
-				Objects.requireNonNull(arg);
-				if (arg instanceof Named) {
-					put(((Named) arg).getName(), arg);
-				}
-			}
-		}
-	}
-	
-	private static final class FmtStateMachine {
-		final StringBuilder str;
-		
-		String fmt;
-		Object[] args;
-		int currentIdx;
-		final NameMap named;
-		int lastCh, endPos = 0;
-
-		FmtStateMachine(String fmt, Object... args) {
-			this(fmt, 0, args, new NameMap(args), '\0');
-		}
-		
-		FmtStateMachine(String fmt, int idx, Object[] args, NameMap named, int lastCh) {
-			this.str = new StringBuilder(fmt.length());
-			this.fmt = fmt;
-			this.currentIdx = idx;
-			this.args = args;
-			this.named = named;
-			this.lastCh = lastCh;
-		}
-		
-		FmtStateMachine formatMain() {
-			int lpos = 0;
-			for (int pos = fmt.indexOf('{'); pos != -1 && hasMore(pos); lpos = pos+1, pos = fmt.indexOf('{', lpos)) {
-				str.append(fmt.substring(lpos, pos).replaceAll("}}", "}"));
-				int epos = fmt.indexOf('}', pos);
-
-				if (fmt.charAt(pos+1) == '{') { 
-					// Literal '{'
-					str.append('{');
-					epos = pos + 1;
-				} else if (epos == pos+1) {
-					// Unmarked '{}' -> get the next argument
-					str.append(getToken(null));
-				} else {
-					epos = formatToken(pos, epos);
-				}
-				pos = epos;
-			}
-			str.append(remaining(lpos).replaceAll("}}", "}"));
-			return this;
-		}
-
-		private String remaining(int lpos) {
-			this.endPos = fmt.indexOf(lastCh, lpos);
-			return lastCh == '\0' ? fmt.substring(lpos) : fmt.substring(lpos, endPos++);
-		}
-
-		private boolean hasMore(int pos) {
-			return lastCh == '\0' || pos < fmt.indexOf(lastCh);
-		}
-		
-		private int formatToken(int pos, int epos) {
-			final String token = fmt.substring(++pos, epos);
-			// If token contains a '.', use reflection?
-			// If token starts with '%', make it a c-format expression
-			if (token.indexOf('?') != -1) {
-				final int b1 = fmt.indexOf('?', pos)+1;
-				boolean _if = getConditionToken(token, b1-pos-1);
-				FmtStateMachine then = new FmtStateMachine(fmt.substring(b1), currentIdx, args, named, ':').formatMain();
-				FmtStateMachine els = new FmtStateMachine(fmt.substring(then.endPos+b1), then.currentIdx, args, named, '}').formatMain();
-				currentIdx = els.currentIdx;
-				epos = els.endPos+then.endPos+b1-1;
-				str.append(_if ? then : els);
-			} else {
-				str.append(getToken(token));
-			}
-			return epos;
-		}
-
-		private boolean getConditionToken(final String token, final int end) {
-			final String cond = token.substring(0, end);
-			Matcher matcher = Pattern.compile("^(\\d*|[a-zA-Z_]+)(>=?|<=?|!=|==?)(\\d+)$").matcher(cond);
-			if (matcher.matches()) {
-				final int cmp = ((Integer) getToken(matcher.group(1))).compareTo(Integer.valueOf(matcher.group(3)));
-				switch (matcher.group(2)) {
-				case "=" : return cmp == 0;
-				case "==": return cmp == 0;
-				case "!=": return cmp != 0;
-				case "<" : return cmp <  0;
-				case "<=": return cmp <= 0;
-				case ">" : return cmp >  0;
-				case ">=": return cmp >= 0;
-				default: throw new IllegalStateException();
-				}
-			} else {
-				return (Boolean) getToken(cond);
-			}
-		}
-
-		private Object getToken(final String token) {
-			if (token == null || token.isEmpty()) {
-				return args[currentIdx++];
-			} else if (Character.isDigit(token.charAt(0))) {
-				return args[Integer.parseInt(token)];
-			} else {
-				return named.get(token);
-			}
-		}
-		
-		public String toString() {
-			return str.toString();
-		}
-	}
-	
-	String fmt;
-	
-	public StringFormatter(String logFmtString) {
-		Objects.requireNonNull(logFmtString);
-		this.fmt = logFmtString;
-	}
-	
-	public String format(Object... args) {
-		return new FmtStateMachine(fmt, args).formatMain().toString();
-	}
-}

+ 0 - 43
src/main/lombok/org/leumasjaffe/format/StringHelper.java

@@ -1,43 +0,0 @@
-package org.leumasjaffe.format;
-
-import lombok.experimental.UtilityClass;
-
-@UtilityClass
-public class StringHelper {
-	public String toString(Object o) {
-		if ( o == null ) { return ""; }
-		else { return o.toString(); }
-	}
-	
-	public String toString(float f) {
-		return Float.toString(f);
-	}
-	
-	public String toString(int i) {
-		return Integer.toString(i);
-	}
-	
-	public String toString(int i, int ignore, String elseValue) {
-		if ( i == ignore ) { return elseValue; }
-		else { return Integer.toString(i); }
-	}
-
-	public String toString(int i, int ignore) {
-		if ( i == ignore ) { return ""; }
-		else { return Integer.toString(i); }
-	}
-	
-	public String toSignedString(int i) {
-		if ( i > 0 ) { return "+" + i; }
-		else { return toString(i); }
-	}
-	
-	public String toSignedString(int i, int ignore) {
-		if ( i == ignore ) { return ""; }
-		else { return toSignedString(i); }
-	}
-	
-	public String format(String logFmtString, Object... args) {
-		return new StringFormatter(logFmtString).format(args);
-	}
-}

+ 0 - 34
src/test/java/org/leumasjaffe/format/StringFormatterTest.java

@@ -1,34 +0,0 @@
-package org.leumasjaffe.format;
-
-import static org.junit.Assert.*;
-
-import org.junit.Test;
-
-public class StringFormatterTest {
-
-	@Test
-	public void testFormatLiteral() {
-		assertEquals("{}", new StringFormatter("{{}}").format());
-	}
-	
-	@Test
-	public void testFormatNumbered() {
-		assertEquals("1,0", new StringFormatter("{1},{0}").format(0, 1));
-	}
-	
-	@Test
-	public void testFormatCondition() {
-		assertEquals("1", new StringFormatter("{?{}:{}}").format(false, 0, 1));
-	}
-	
-	@Test
-	public void testFormatNumberedCondition() {
-		assertEquals("0 word + 1 word/2 levels", StringHelper.format("{0} {2} + {1} {2}/{3?level:{4} levels}", 0, 1, "word", false, 2));
-	}
-	
-	@Test
-	public void testFormatIntegerCompareCondition() {
-		assertEquals("true", StringHelper.format("{>1?true:false}", 2));
-	}
-
-}