Skip to content

Commit

Permalink
an attempt to make the UnaryFunction nature of Cursive more visible;
Browse files Browse the repository at this point in the history
  • Loading branch information
jim committed Jun 10, 2015
1 parent 4128902 commit fb6193a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 71 deletions.
5 changes: 4 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

<groupId>bbcursive</groupId>
<artifactId>bbcursive</artifactId>
<version>0.1.0.2</version>
<version>0.1.0.3</version>
<packaging>jar</packaging>

<name>bbcursive</name>
<url>http://maven.apache.org</url>


<properties>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
Expand Down
92 changes: 45 additions & 47 deletions src/main/java/bbcursive/Cursive.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,57 +18,57 @@
* </pre>
*/
public interface Cursive {
<T extends ByteBuffer> T f(T target);
ByteBuffer apply(ByteBuffer target);

enum pre implements Cursive {
duplicate {

public <T extends ByteBuffer> T f(T target) {
return (T) target.duplicate();
public ByteBuffer apply(ByteBuffer target) {
return target.duplicate();
}
}, flip {

public <T extends ByteBuffer> T f(T target) {
return (T) target.flip();
public ByteBuffer apply(ByteBuffer target) {
return ( ByteBuffer ) target.flip();
}
}, slice {

public <T extends ByteBuffer> T f(T target) {
return (T) target.slice();
public ByteBuffer apply(ByteBuffer target) {
return target.slice();
}
}, mark {

public <T extends ByteBuffer> T f(T target) {
return (T) target.mark();
public ByteBuffer apply(ByteBuffer target) {
return ( ByteBuffer ) target.mark();
}
}, reset {

public <T extends ByteBuffer> T f(T target) {
return (T) target.reset();
public ByteBuffer apply(ByteBuffer target) {
return ( ByteBuffer ) target.reset();
}
},
/**
* exists in both pre and post Cursive atoms.
*/
rewind {

public <T extends ByteBuffer> T f(T target) {
return (T) target.rewind();
public ByteBuffer apply(ByteBuffer target) {
return ( ByteBuffer ) target.rewind();
}
},
/**
* rewinds, dumps to console but returns unchanged buffer
*/
debug {

public <T extends ByteBuffer> T f(T target) {
public ByteBuffer apply(ByteBuffer target) {
System.err.println("%%: " + std.str(target, duplicate, rewind));
return target;
}
}, ro {

public <T extends ByteBuffer> T f(T target) {
return (T) target.asReadOnlyBuffer();
public ByteBuffer apply(ByteBuffer target) {
return target.asReadOnlyBuffer();
}
},

Expand All @@ -81,12 +81,10 @@ public <T extends ByteBuffer> T f(T target) {


forceSkipWs {

public <T extends ByteBuffer> T f(T target) {
public ByteBuffer apply(ByteBuffer target) {
int position = target.position();

while (target.hasRemaining() && Character.isWhitespace(target.get())) {
}
while (target.hasRemaining() && Character.isWhitespace(target.get()));
if (!target.hasRemaining()) {
target.position(position);
throw new BufferUnderflowException();
Expand All @@ -96,7 +94,7 @@ public <T extends ByteBuffer> T f(T target) {
},
skipWs {

public <T extends ByteBuffer> T f(T target) {
public ByteBuffer apply(ByteBuffer target) {
int position = target.position();

while (target.hasRemaining() && Character.isWhitespace(target.get())) {
Expand All @@ -107,7 +105,7 @@ public <T extends ByteBuffer> T f(T target) {
},
toWs {

public <T extends ByteBuffer> T f(T target) {
public ByteBuffer apply(ByteBuffer target) {
while (target.hasRemaining() && !Character.isWhitespace(target.get())) {
}
return target;
Expand All @@ -118,7 +116,7 @@ public <T extends ByteBuffer> T f(T target) {
*/
forceToEol {

public <T extends ByteBuffer> T f(T target) {
public ByteBuffer apply(ByteBuffer target) {
while (target.hasRemaining() && '\n' != target.get()) {
}
if (!target.hasRemaining()) {
Expand All @@ -132,38 +130,38 @@ public <T extends ByteBuffer> T f(T target) {
*/
toEol {

public <T extends ByteBuffer> T f(T target) {
public ByteBuffer apply(ByteBuffer target) {
while (target.hasRemaining() && '\n' != target.get()) {
}
return target;
}
},
back1 {

public <T extends ByteBuffer> T f(T target) {
public ByteBuffer apply(ByteBuffer target) {
int position = target.position();
return (T) (0 < position ? target.position(position - 1) : target);
return ( ByteBuffer ) (0 < position ? target.position(position - 1) : target);
}
},
/**
* reverses position _up to_ 2.
*/
back2 {

public <T extends ByteBuffer> T f(T target) {
public ByteBuffer apply(ByteBuffer target) {
int position = target.position();
return (T) (1 < position ? target.position(position - 2) : bb(target, back1));
return ( ByteBuffer ) (1 < position ? target.position(position - 2) : bb(target, back1));
}
}, /**
* reduces the position of target until the character is non-white.
*/rtrim {

public <T extends ByteBuffer> T f(T target) {
public ByteBuffer apply(ByteBuffer target) {
int start = target.position(), i = start;
while (0 <= --i && Character.isWhitespace(target.get(i))) {
}

return (T) target.position(++i);
return ( ByteBuffer ) target.position(++i);
}
},

Expand All @@ -172,12 +170,12 @@ public <T extends ByteBuffer> T f(T target) {
*/
noop {

public <T extends ByteBuffer> T f(T target) {
public ByteBuffer apply(ByteBuffer target) {
return target;
}
}, skipDigits {

public <T extends ByteBuffer> T f(T target) {
public ByteBuffer apply(ByteBuffer target) {
while (target.hasRemaining() && Character.isDigit(target.get())) {
}
return target;
Expand All @@ -188,35 +186,35 @@ public <T extends ByteBuffer> T f(T target) {
enum post implements Cursive {
compact {

public <T extends ByteBuffer> T f(T target) {
return (T) target.compact();
public ByteBuffer apply(ByteBuffer target) {
return target.compact();
}
}, reset {

public <T extends ByteBuffer> T f(T target) {
return (T) target.reset();
public ByteBuffer apply(ByteBuffer target) {
return ( ByteBuffer ) target.reset();
}
}, rewind {

public <T extends ByteBuffer> T f(T target) {
return (T) target.rewind();
public ByteBuffer apply(ByteBuffer target) {
return ( ByteBuffer ) target.rewind();
}
}, clear {

public <T extends ByteBuffer> T f(T target) {
return (T) target.clear();
public ByteBuffer apply(ByteBuffer target) {
return ( ByteBuffer ) target.clear();
}

}, grow {

public <T extends ByteBuffer> T f(T target) {
return (T) std.grow(target);
public ByteBuffer apply(ByteBuffer target) {
return std.grow(target);
}

}, ro {

public <T extends ByteBuffer> T f(T target) {
return (T) target.asReadOnlyBuffer();
public ByteBuffer apply(ByteBuffer target) {
return target.asReadOnlyBuffer();
}
},
/**
Expand All @@ -225,7 +223,7 @@ public <T extends ByteBuffer> T f(T target) {

pad0 {

public <T extends ByteBuffer> T f(T target) {
public ByteBuffer apply(ByteBuffer target) {
while (target.hasRemaining()) {
target.put((byte) 0);
}
Expand All @@ -238,13 +236,13 @@ public <T extends ByteBuffer> T f(T target) {

pad0Until {

public <T extends ByteBuffer> T f(T target) {
public ByteBuffer apply(ByteBuffer target) {
int limit = target.limit();
target.flip();
while (target.hasRemaining()) {
target.put((byte) 0);
}
return (T) target.limit(limit);
return ( ByteBuffer ) target.limit(limit);
}
}
}
Expand Down
41 changes: 18 additions & 23 deletions src/main/java/bbcursive/std.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package bbcursive;

import bbcursive.Cursive.post;
import bbcursive.Cursive.pre;
import com.databricks.fastbuffer.ByteBufferReader;
import com.databricks.fastbuffer.JavaByteBufferReader;
Expand All @@ -18,33 +17,33 @@
* Created by jim on 8/8/14.
*/
public class std {
public static <T extends ByteBuffer> T bb(T b, Cursive... ops) {
public static ByteBuffer bb(ByteBuffer b, Cursive... ops) {
for (int i = 0, opsLength = ops.length; i < opsLength; i++) {
Cursive op = ops[i];
b = op.f(b);
b = op.apply(b);
}
return b;
}

public static <T extends ByteBuffer, S extends WantsZeroCopy> T bb(S b, Cursive... ops) {
public static < S extends WantsZeroCopy> ByteBuffer bb(S b, Cursive... ops) {
ByteBuffer b1 = b.asByteBuffer();
for (int i = 0, opsLength = ops.length; i < opsLength; i++) {
Cursive op = ops[i];
b1 = op.f(b1);
b1 = op.apply(b1);
}
return (T) b1;
return b1;
}

public static <T extends ByteBufferReader, S extends WantsZeroCopy> T fast(S zc) {
public static <S extends WantsZeroCopy> ByteBufferReader fast(S zc) {
return fast(zc.asByteBuffer());
}

public static <T extends ByteBufferReader> T fast(ByteBuffer buf) {
T r;
public static ByteBufferReader fast(ByteBuffer buf) {
ByteBufferReader r;
try {
r = (T) (buf.hasArray() ? new UnsafeHeapByteBufferReader(buf) : new UnsafeDirectByteBufferReader(buf));
r = (ByteBufferReader) (buf.hasArray() ? new UnsafeHeapByteBufferReader(buf) : new UnsafeDirectByteBufferReader(buf));
} catch (UnsupportedOperationException e) {
r = (T) new JavaByteBufferReader(buf);
r = new JavaByteBufferReader(buf);
}
return r;
}
Expand All @@ -59,13 +58,13 @@ public static <T extends ByteBufferReader> T fast(ByteBuffer buf) {
public static String str(ByteBuffer bytes, Cursive... operations) {
for (Cursive operation : operations) {
if (operation instanceof pre) {
bytes = operation.f(bytes);
bytes = operation.apply(bytes);
}
}
String s = UTF_8.decode(bytes).toString();
for (Cursive operation : operations) {
if (!(operation instanceof pre)) {
bytes = operation.f(bytes);
bytes = operation.apply(bytes);
}
}
return s;
Expand Down Expand Up @@ -150,8 +149,6 @@ public static Integer parseInt(ByteBuffer r) {

}
}


res = (int) ((neg ? -x : x) & 0xffffffffL);
}
return res;
Expand All @@ -167,7 +164,7 @@ public static <T extends CharSequence> ByteBuffer bb(T src, Cursive... operation

ByteBuffer byteBuffer = UTF_8.encode(src.toString());
for (Cursive operation : operations) {
byteBuffer = operation.f(byteBuffer);
byteBuffer = operation.apply(byteBuffer);
}
return byteBuffer;
}
Expand Down Expand Up @@ -233,25 +230,23 @@ public static Integer parseInt(String r)
/**
* @param src
* @param dest
* @param <R>
* @param <S>
* @return
*/

public static <R extends ByteBuffer, S extends ByteBuffer> R push(S src, R dest) {
public static ByteBuffer push(ByteBuffer src, ByteBuffer dest) {
int need = src
.remaining(),
have = dest.remaining();
if (have > need) {
return (R) dest.put(src);
return (ByteBuffer) dest.put(src);
}
dest.put((S) src.slice().limit(have));
dest.put((ByteBuffer) src.slice().limit(have));
src.position(src.position() + have);
return dest;
}

public static <T extends ByteBuffer, S extends ByteBuffer> T grow(S src) {
return (T) ByteBuffer.allocateDirect(src.capacity() << 1).put(src);
public static ByteBuffer grow(ByteBuffer src) {
return ByteBuffer.allocateDirect(src.capacity() << 1).put(src);
}

/**
Expand Down

0 comments on commit fb6193a

Please sign in to comment.