Browse Source

StrBuilder - refactoring / encapsulation

Ranides Atterwim 10 years ago
parent
commit
ad6e286398

+ 1 - 1
assira/src/main/java/net/ranides/assira/reflection/impl/AClass.java

@@ -154,7 +154,7 @@ public class AClass implements IClass {
         if(params.isEmpty()) {
             return name();
         }
-        return new StrBuilder().append(name()).open("<",">",", ").append(params).close().toString();
+        return StrBuilder.alloc().append(name()).open("<",">",", ").append(params).close().toString();
     }
     
 }

+ 1 - 1
assira/src/main/java/net/ranides/assira/reflection/impl/AMethod.java

@@ -78,7 +78,7 @@ public abstract class AMethod implements IMethod {
     
     @Override
     public String toString() {
-        return new StrBuilder()
+        return StrBuilder.alloc()
             .append(returns()).append(" ")
             .append(parent())
             .append(".")

+ 0 - 551
assira/src/main/java/net/ranides/assira/text/StrBuffer.java

@@ -1,551 +0,0 @@
-/*
- * @author Ranides Atterwim <ranides@gmail.com>
- * @copyright Ranides Atterwim
- * @license WTFPL
- * @url http://ranides.net/projects/assira
- */
-package net.ranides.assira.text;
-
-import java.io.Reader;
-import java.io.Serializable;
-import java.io.Writer;
-import java.util.Iterator;
-import java.util.function.Function;
-import java.util.function.IntFunction;
-import net.ranides.assira.collection.arrays.ArrayUtils;
-import net.ranides.assira.collection.arrays.NativeArray;
-import net.ranides.assira.collection.arrays.NativeArrayUtils;
-import net.ranides.assira.math.MathUtils;
-
-/**
- *
- * @author Ranides Atterwim <ranides@gmail.com>
- */
-@SuppressWarnings({
-    "PMD.TooManyPublicMethods",
-    "PMD.TooManyDifferentMethods"
-})
-public class StrBuffer implements CharSequence, Appendable, Serializable {
-    
-    private static final long serialVersionUID = 1L;
-    
-    protected static final char[] EMPTY = new char[0];
-    
-    protected char[] buffer;
-    
-    protected int size;
-    
-    private final BuilderState options = new BuilderState();
-    
-    public StrBuffer(int capacity) {
-        buffer = new char[capacity];
-    }
-    
-    public StrBuilderOptions options() {
-        return options;
-    }
-    
-    @Override
-    public int length() {
-        return size;
-    }
-
-    public int capacity() {
-        return buffer.length;
-    }
-    
-    public StrBuffer resize(int length, char c) {
-        if (length < 0) {
-            throw new IllegalArgumentException("negative size: " + length);
-        }
-        if (length > size) {
-            throw new IllegalArgumentException(length + " is greater than " + capacity());
-        }
-        size = length;
-        return this;
-    }
-    
-    public StrBuffer reserve(int capacity) {
-        throw new UnsupportedOperationException();
-    }
-    
-    public StrBuffer trim() {
-        throw new UnsupportedOperationException();
-    }
-    
-    public StrBuffer trim(int n) {
-        throw new UnsupportedOperationException();
-    }
-    
-    public StrBuffer clear() {
-        size = 0;
-        return this;
-    }
-    
-    @Override
-    public char charAt(int index) {
-        return buffer[index];
-    }
-    
-    public StrBuffer setCharAt(int index, char value) {
-        buffer[index] = value;
-        return this;
-    }
-
-    @Override
-    public CharSequence subSequence(int begin, int end) {
-        return StringUtils.wrap(buffer, begin, end);
-    }
-        
-    public char[] getChars() {
-        return getChars(0, size);
-    }
-    
-    public char[] getChars(int begin, int end) {
-        if (end == begin) {
-            return StrBuffer.EMPTY;
-        }
-        return ArrayUtils.slice(buffer, begin, end);
-    }
-    
-    public char[] getChars(char[] target) {
-        return getChars(0, size, target, 0);
-    }
-    
-    public char[] getChars(int begin, int end, char target[], int offset) {
-        System.arraycopy(buffer, begin, target, offset, end - begin);
-        return target;
-    }
-    
-    public String getRightFragment(int length) {
-        return new String(buffer, size - length, length);
-    }
-    
-    public String getLeftFragment(int length) {
-        return new String(buffer, 0, length);
-    }
-    
-    public StrBuffer reverse() {
-        NativeArrayUtils.reverse(NativeArray.wrap(buffer), size);
-        return this;
-    }
-    
-    public StrBuffer open() {
-        return StrBuffer.this.append(options.reset().open());
-    }
-    
-    public StrBuffer open(String open) {
-        options.open(open);
-        return open();
-    }
-    
-    public StrBuffer open(String open, String close) {
-        options.open(open).close(close);
-        return open();
-    }
-    
-    public StrBuffer open(String open, String close, String separator) {
-        options.open(open).close(close).separator(separator);
-        return open();
-    }
-    
-    public StrBuffer close() {
-        return StrBuffer.this.append(options.close());
-    }
-    
-    public StrBuffer item() {
-        if(0 != options.counter++) {
-            StrBuffer.this.append(options.separator());
-        }
-        return this;
-    }
-    
-    public StrBuffer endl() {
-        return StrBuffer.this.append(options.endl());
-    }
-    
-    public StrBuffer append() {
-        return this;
-    }
-
-    @Override
-    public StrBuffer append(CharSequence value) {
-        return StrBuffer.this.append(value, 0, value.length());
-    }
-
-    @Override
-    public StrBuffer append(CharSequence value, int begin, int end) {
-        for(int i=begin; i<end; i++) {
-            buffer[size++] = value.charAt(i);
-        }
-        return this;
-    }
-    
-    public StrBuffer append(String value) {
-        return StrBuffer.this.append(value, 0, value.length());
-    }
-    
-    public StrBuffer append(String value, int begin, int end) {
-        int n = end - begin;
-        value.getChars(begin, end, buffer, size);
-        size += n;
-        return this;
-    }
-    
-    public StrBuffer append(StringBuffer value) {
-        return StrBuffer.this.append(value, 0, value.length());
-    }
-    
-    public StrBuffer append(StringBuffer value, int begin, int end) {
-        int n = end - begin;
-        value.getChars(begin, end, buffer, size);
-        size += n;
-        return this;
-    }
-    
-    public StrBuffer append(StringBuilder value) {
-        return StrBuffer.this.append(value, 0, value.length());
-    }
-    
-    public StrBuffer append(StringBuilder value, int begin, int end) {
-        int n = end - begin;
-        value.getChars(begin, end, buffer, size);
-        size += n;
-        return this;
-    }
-    
-    public StrBuffer append(char[] value) {
-        return StrBuffer.this.append(value, 0, value.length);
-    }
-    
-    public StrBuffer append(char[] value, int begin, int end) {
-        int n = end - begin;
-        System.arraycopy(value, begin, buffer, size, n);
-        size += n;
-        return this;
-    }
-
-    @Override
-    public StrBuffer append(char c) {
-        buffer[size++] = c;
-        return this;
-    }
-
-    public StrBuffer append(Object value) {
-        return StrBuffer.this.append(value.toString());        
-    }
-    
-    public StrBuffer append(boolean value) {
-        return StrBuffer.this.append(value ? "true" : "false");
-    }
-    
-    public StrBuffer append(int value) {
-        return StrBuffer.this.append(String.valueOf(value));
-    }
-
-    public StrBuffer append(long value) {
-        return StrBuffer.this.append(String.valueOf(value));
-    }
-
-    public StrBuffer append(float value) {
-        return StrBuffer.this.append(String.valueOf(value));
-    }
-
-    public StrBuffer append(double value) {
-        return StrBuffer.this.append(String.valueOf(value));
-    }
-    
-    public StrBuffer append(Iterable<?> values) {
-		return StrBuffer.this.append(values.iterator(), Object::toString);
-	}
-    
-    public <T> StrBuffer append(Iterable<? extends T> values, Function<T,String> function) {
-		return StrBuffer.this.append(values.iterator(), function);
-	}
-    
-    public <T> StrBuffer append(Iterator<? extends T> values) {
-        return StrBuffer.this.append(values, Object::toString);
-    }
-        
-    public <T> StrBuffer append(Iterator<? extends T> values, Function<T,String> function) {
-        if ( !values.hasNext() ) {
-            return this;
-        }
-        String separator = options().separator();
-        for (;;) {
-            T item = values.next();
-            StrBuffer.this.append(function.apply(item));
-            if ( !values.hasNext() ) {
-                return this;
-            }
-            StrBuffer.this.append(separator);
-        }
-	}
-    
-    public <T> StrBuffer append(T[] values) {
-        return StrBuffer.this.append(values, Object::toString);
-    }
-    
-    public <T> StrBuffer append(T[] values, Function<T,String> function) {
-        if ( 0==values.length ) {
-            return this;
-        }
-        String separator = options().separator();
-		StrBuffer.this.append(function.apply(values[0]));
-        for (int i=1; i<values.length; i++) {
-            StrBuffer.this.append(separator).append(function.apply(values[i]));
-        }
-		return this;
-	}
-    
-    public <T> StrBuffer append(NativeArray values) {
-        return StrBuffer.this.append(values.size(), i -> String.valueOf(values.get(i)));
-    }
-    
-    public <T> StrBuffer append(int size, IntFunction<String> function) {
-        if ( 0==size ) {
-            return this;
-        }
-        String separator = options().separator();
-        StrBuffer.this.append(function.apply(0));
-        for (int i=1; i<size; i++) {
-            StrBuffer.this.append(separator).append(function.apply(i));
-        }
-		return this;
-	}
-    
-    @Override
-    public boolean equals(Object object) {
-        if (this == object) {
-            return true;
-        }
-        if (object instanceof StrBuffer) {
-            return equalsSB((StrBuffer)object);
-        }
-        if (object instanceof CharSequence) {
-            return equalsCS((CharSequence)object);
-        }
-        return false;
-    }
-
-    private boolean equalsSB(StrBuffer other) {
-        if (this.size != other.size) {
-            return false;
-        }
-        for(int i=0; i<size; i++) {
-            if(this.buffer[i] != other.buffer[i]) {
-                return false;
-            }
-        }
-        return true;
-    }
-
-    private boolean equalsCS(CharSequence other) {
-        if (this.size != other.length()) {
-            return false;
-        }
-        for(int i=0; i<size; i++) {
-            if(this.buffer[i] != other.charAt(i)) {
-                return false;
-            }
-        }
-        return true;
-    }
-
-    @Override
-    public int hashCode() {
-        return NativeArray.wrap(buffer).hashCode(0, size);
-    }
-    
-    @Override
-    public String toString() {
-        return new String(buffer, 0, size);
-    }
-    
-    public StringBuilder toBuilder() {
-        return new StringBuilder(size).append(buffer, 0, size);
-    }
-    
-    public Writer asWriter() {
-        return new SWriter();
-    }
-    
-    public Reader asReader() {
-        return new SReader();
-    }
-    
-    private final class SReader extends Reader {
-        
-        private int pos;
-        private int mark;
-
-        @Override
-        public void close() {
-            // do nothing
-        }
-        
-        @Override
-        public int read() {
-            if (!ready()) {
-                return -1;
-            }
-            return buffer[pos++];
-        }
-        
-        @Override
-        public int read(char target[], int offset, int length) {
-            if (pos >= length()) {
-                return -1;
-            }
-            if (pos + length > length()) {
-                length = length() - pos;
-            }
-            StrBuffer.this.getChars(pos, pos + length, target, offset);
-            pos += length;
-            return length;
-        }
-        
-        @Override
-        public long skip(long n) {
-            if (pos + n > size) {
-                n = size - pos;
-            }
-            if (n < 0) {
-                return 0;
-            }
-            pos += n;
-            return n;
-        }
-        
-        @Override
-        public boolean ready() {
-            return pos < size;
-        }
-        
-        @Override
-        public boolean markSupported() {
-            return true;
-        }
-        
-        @Override
-        public void mark(int readAheadLimit) {
-            mark = pos;
-        }
-        
-        @Override
-        public void reset() {
-            pos = mark;
-        }
-
-    }
-
-    private final class SWriter extends Writer {
-        
-        @Override
-        public void close() {
-            // do nothing
-        }
-        
-        @Override
-        public void flush() {
-            // do nothing
-        }
-        
-        @Override
-        public void write(int c) {
-            StrBuffer.this.append((char) c);
-        }
-        
-        @Override
-        public void write(char[] buffer) {
-            StrBuffer.this.append(buffer);
-        }
-        
-        @Override
-        public void write(char[] buffer, int offset, int length) {
-            StrBuffer.this.append(buffer, offset, length);
-        }
-        
-        @Override
-        public void write(String str) {
-            StrBuffer.this.append(str);
-        }
-        
-        @Override
-        public void write(String str, int offset, int length) {
-            StrBuffer.this.append(str, offset, length);
-        }
-        
-    }
-    
-    private final class BuilderState extends StrBuilderOptions {
-        
-        private static final long serialVersionUID = 1L;
-        
-        protected int counter;
-
-        private String endl;
-
-        private String separator;
-
-        private String open;
-
-        private String close;
-        
-        @Override
-        public StrBuffer builder() {
-            return StrBuffer.this;
-        }
-
-        @Override
-        public String endl() {
-            return endl;
-        }
-
-        @Override
-        public BuilderState endl(String value) {
-            this.endl = value;
-            return this;
-        }
-
-        @Override
-        public String separator() {
-            return separator;
-        }
-
-        @Override
-        public BuilderState separator(String value) {
-            this.separator = value;
-            return this;
-        }
-
-        @Override
-        public String open() {
-            return open;
-        }
-
-        @Override
-        public BuilderState open(String value) {
-            this.open = value;
-            return this;
-        }
-
-        @Override
-        public String close() {
-            return close;
-        }
-
-        @Override
-        public BuilderState close(String value) {
-            this.close = value;
-            return this;
-        }
-        
-        protected BuilderState reset() {
-            this.counter = 0;
-            return this;
-        }
-        
-    }
-    
-}

File diff suppressed because it is too large
+ 1002 - 289
assira/src/main/java/net/ranides/assira/text/StrBuilder.java


+ 1 - 1
assira/src/main/java/net/ranides/assira/text/StrBuilderOptions.java

@@ -22,7 +22,7 @@ public class StrBuilderOptions implements Serializable {
         // do nothing
     }
     
-    public StrBuffer builder() {
+    public StrBuilder builder() {
         throw new UnsupportedOperationException();
     }
     

+ 1 - 1
assira/src/test/java/net/ranides/assira/collection/maps/CacheTest.java

@@ -257,7 +257,7 @@ public class CacheTest {
     }
 	
 	private static String text(Random rand, int size) {
-		StrBuilder sb = new StrBuilder(size);
+		StrBuilder sb = StrBuilder.alloc(size);
 		for(int i=0; i<size; i++) {
 			sb.append( (char)(' '+rand.nextInt(128)) );
 		}

+ 26 - 26
assira/src/test/java/net/ranides/assira/text/StrBuilderTest.java

@@ -33,7 +33,7 @@ public class StrBuilderTest {
         List<Integer> list2 = Arrays.asList(10,11,12,13);
         List<Integer> list3 = Arrays.asList(3,4,5);
         
-        StrBuilder sb = new StrBuilder();
+        StrBuilder sb = StrBuilder.alloc();
         sb
             .options()
                 .open("{ ").close(" }").separator(",")
@@ -66,7 +66,7 @@ public class StrBuilderTest {
     
     @Test
     public void testOpenClose() {
-        StrBuilder sb = new StrBuilder();
+        StrBuilder sb = StrBuilder.alloc();
         
         assertThrows(IllegalStateException.class, sb::close);
         
@@ -89,7 +89,7 @@ public class StrBuilderTest {
     
     @Test
     public void testOptions() {
-        StrBuilder sb = new StrBuilder();
+        StrBuilder sb = StrBuilder.alloc();
         
         sb
             .options().blank("b1").endl("o1").separator("c1").builder()
@@ -120,7 +120,7 @@ public class StrBuilderTest {
     
     @Test
     public void testAppend_String() {
-        StrBuilder builder = new StrBuilder();
+        StrBuilder builder = StrBuilder.alloc();
         builder.options().blank("-");
         
         // String
@@ -162,7 +162,7 @@ public class StrBuilderTest {
     
     @Test
     public void testAppend_Chars() {
-        StrBuilder builder = new StrBuilder();
+        StrBuilder builder = StrBuilder.alloc();
         builder.options().blank("-");
         
         // char[]
@@ -182,7 +182,7 @@ public class StrBuilderTest {
     
     @Test
     public void testAppend_Primitive() {
-        StrBuilder builder = new StrBuilder();
+        StrBuilder builder = StrBuilder.alloc();
 
         // boolean
         builder
@@ -202,7 +202,7 @@ public class StrBuilderTest {
     
     @Test
     public void testAppend_Iterable() {
-        StrBuilder builder = new StrBuilder();
+        StrBuilder builder = StrBuilder.alloc();
         builder.options()
             .separator(",")
             .blank("-")
@@ -239,7 +239,7 @@ public class StrBuilderTest {
     
     @Test
     public void testAppend_Array() {
-        StrBuilder builder = new StrBuilder();
+        StrBuilder builder = StrBuilder.alloc();
         builder.options()
             .separator(",")
             .blank("-")
@@ -277,7 +277,7 @@ public class StrBuilderTest {
     
     @Test
     public void testReserve() {
-        StrBuilder sb = new StrBuilder(128);
+        StrBuilder sb = StrBuilder.alloc(128);
         sb.append("Hello world");
         assertEquals(128, sb.capacity());
         assertEquals(11, sb.length());
@@ -292,7 +292,7 @@ public class StrBuilderTest {
     
     @Test
     public void testTrim() {
-        StrBuilder sb = new StrBuilder(128);
+        StrBuilder sb = StrBuilder.alloc(128);
         sb.append("Hello world");
         assertEquals(128, sb.capacity());
         assertEquals(11, sb.length());
@@ -310,7 +310,7 @@ public class StrBuilderTest {
     
     @Test
     public void testResize() {
-        StrBuilder sb = new StrBuilder(128);
+        StrBuilder sb = StrBuilder.alloc(128);
         sb.append("Hello world");
 
         sb.resize(8, '.');
@@ -329,7 +329,7 @@ public class StrBuilderTest {
     
     @Test
     public void testClear() {
-        StrBuilder sb = new StrBuilder(8);
+        StrBuilder sb = StrBuilder.alloc(8);
         sb.append("Hello world");
         assertEquals(16, sb.capacity());
         assertEquals(11, sb.length());
@@ -342,7 +342,7 @@ public class StrBuilderTest {
     
     @Test
     public void testSequence() {
-        StrBuffer sb = new StrBuilder().append("Hello world");
+        StrBuilder sb = StrBuilder.alloc().append("Hello world");
         
         StringTester.assertString("Hello world", sb);
         StringTester.assertString("ello wo", sb.subSequence(1, 8));
@@ -361,7 +361,7 @@ public class StrBuilderTest {
     
     @Test
     public void testGetChars() {
-        StrBuffer sb = new StrBuilder(32).append("Hello world");
+        StrBuilder sb = StrBuilder.alloc(32).append("Hello world");
         
         assertArrayEquals("Hello world".toCharArray(), sb.getChars());
         
@@ -388,7 +388,7 @@ public class StrBuilderTest {
     
     @Test
     public void testFragmet() {
-        StrBuffer sb = new StrBuilder(32).append("Hello world");
+        StrBuilder sb = StrBuilder.alloc(32).append("Hello world");
         
         assertEquals("", sb.getLeftFragment(0));
         assertEquals("Hell", sb.getLeftFragment(4));
@@ -401,7 +401,7 @@ public class StrBuilderTest {
     
     @Test
     public void testReverse() {
-        StrBuffer sb = new StrBuilder(32).append("Hello world");
+        StrBuilder sb = StrBuilder.alloc(32).append("Hello world");
         
         sb.reverse();
         assertEquals("dlrow olleH", sb.toString());
@@ -414,7 +414,7 @@ public class StrBuilderTest {
     
     @Test
     public void testToBuilder() {
-        StrBuffer sb = new StrBuilder(32).append("Hello world");
+        StrBuilder sb = StrBuilder.alloc(32).append("Hello world");
         assertEquals("Hello world!", sb.toBuilder().append("!").toString());
         assertEquals("Hello world", sb.toString());
     }
@@ -422,17 +422,17 @@ public class StrBuilderTest {
     @Test
     @SuppressFBWarnings("EC_UNRELATED_TYPES")
     public void testEquals() {
-        StrBuffer a = new StrBuilder(16).append("Hello");
-        StrBuffer b = new StrBuilder(160).append("He").append("llo");
-        StrBuffer c = new StrBuilder(160).append("He").append("ll!");
+        StrBuilder a = StrBuilder.alloc(16).append("Hello");
+        StrBuilder b = StrBuilder.alloc(160).append("He").append("llo");
+        StrBuilder c = StrBuilder.alloc(160).append("He").append("ll!");
         
         assertTrue(a.equals(a));
         
         assertFalse(a.equals(null));
         assertFalse(a.equals(new Object()));
         
-        assertFalse(a.equals(new StrBuilder(160).append("Hallo")));
-        assertFalse(a.equals(new StrBuilder(160).append("Hello!")));
+        assertFalse(a.equals(StrBuilder.alloc(160).append("Hallo")));
+        assertFalse(a.equals(StrBuilder.alloc(160).append("Hello!")));
         
         assertFalse(a.equals("Hello!"));
         assertFalse(a.equals("Hella"));
@@ -445,7 +445,7 @@ public class StrBuilderTest {
     
     @Test
     public void testWriter() throws IOException {
-        StrBuilder sb = new StrBuilder();
+        StrBuilder sb = StrBuilder.alloc();
         Writer writer = sb.asWriter();
         sb.append("Hello ");
         
@@ -465,10 +465,10 @@ public class StrBuilderTest {
     
     @Test
     public void testReader() throws IOException {
-        TAdapter<Reader, StrBuffer> map = new TAdapter<>();
+        TAdapter<Reader, StrBuilder> map = new TAdapter<>();
         
         Function<String, Reader> rf = map.map(
-            (String text) -> new StrBuilder().append(text), builder -> builder.asReader()
+            (String text) -> StrBuilder.alloc().append(text), builder -> builder.asReader()
         );
         BiConsumer<Reader, String> ra = (reader, text) -> {
             map.get(reader).append(text);
@@ -482,7 +482,7 @@ public class StrBuilderTest {
     
     @Test
     public void testHashcode() {
-        assertEquals(Arrays.hashCode("Hello".toCharArray()), new StrBuilder(32).append("Hello").hashCode());
+        assertEquals(Arrays.hashCode("Hello".toCharArray()), StrBuilder.alloc(32).append("Hello").hashCode());
     }
 
     

+ 2 - 2
assira/src/test/java/net/ranides/assira/text/StringUtilsBenchmark.java

@@ -310,7 +310,7 @@ public class StringUtilsBenchmark extends SimpleBenchmark {
             prv = Character.toLowerCase(prv);
         }
         
-        StrBuilder builder = new StrBuilder(2*max);
+        StrBuilder builder = StrBuilder.alloc(2*max);
         
         for(int i=1; i<max; i++) {
             char c = value.charAt(i);
@@ -356,7 +356,7 @@ public class StringUtilsBenchmark extends SimpleBenchmark {
             prv = Character.toLowerCase(prv);
         }
         
-        StrBuffer builder = new StrBuffer(2*max);
+        StrBuilder builder = StrBuilder.unsafe(2*max);
         
         for(int i=1; i<max; i++) {
             char c = value.charAt(i);