I'm trying to make a Java program more "Groovy". The java code reads an InputStream like so:
static int myFunction(InputStream is) throws IOException {
int b=is.read();
if (b==0) return b;
StringBuffer sb=new StringBuffer();
int c;
boolean done = false;
while(!done) {
c=is.read();
sb.append((char)c);
if(c == '\n') {
done=true;
}
}
System.out.println(sb.toString());
if (b == 1) throw new IOException("blah");
return b;
}
My Groovy version looks like this:
def myFunction(InputStream is) throws IOException {
int b=is.read()
if (b==0) return b
def reader = new BufferedReader(new InputStreamReader(is))
reader.eachLine { println(it) }
println("DONE")
if (b == 1) throw new IOException("blah")
return b
}
It prints the contents of the stream and then just hangs as if it's trying to read more. It never prints "DONE" (added for debugging). Next I tried it using is.eachByte and passing a closure with an explicit "if (c == '\n') return" but I found that return inside a closure acts more like a continue and doesn't actually break out of the closure. Any idea what I'm doing wrong?
Instead of
reader.eachLine { println(it) }
Can you try
println reader.readLine()
Related
I have an error ("unexpected token"). I don't know Groovy very well , but how to fix it? Part of my code:
def PriorityQueue<Agent> agentPQ = new PriorityQueue<>(agents.size(), new Comparator<Agent>() {
#Override
int compare(Agent o1, Agent o2) {
if(o1.issueCount == o2.issueCount){
if(o2.lastAssignedTime == o1.lastAssignedTime){
return o1.user.name.compareTo(o2.user.name)
}
else{
return o1.lastAssignedTime.compareTo(o2.lastAssignedTime)
}
}
else{
return o1.issueCount - o2.issueCount
}
}
})
I have an error in < case
Your problem is caused by HTML entities that encoded < and > characters:
def PriorityQueue<Agent> agentPQ = new PriorityQueue<>(agents.size(), new Comparator<Agent>() {
It should be:
def PriorityQueue<Agent> agentPQ = new PriorityQueue<>(agents.size(), new Comparator<Agent>() {
The code you have pasted wont compile neither in Groovy nor in Java.
In Groovy, I can overload operator '+' plus as follow:
class MutableInt {
int val
MutableInt(int val) { this.val = val }
MutableInt plus(int val) {
return new MutableInt(this.val += val)
}
}
The above class works fine for the following test cases:
def m1 = new MutableInt(1);
assert (m1 + 1).val == 2;
However, if I need to use it together with Map like this and compile it with static
#groovy.transform.CompileStatic
void compileItWithStatic() {
Map<Long, MutableInt> mutMap = [:].withDefault{ new MutableInt(0) }
assert (mutMap[1L] += 20).val == 20;
}
compileItWithStatic()
I got the following error:
*Script1.groovy: 17: [Static type checking] -
Cannot call <K,V> java.util.Map <java.lang.Long, MutableInt>#putAt(java.lang.Long, MutableInt) with arguments [long, int]*
How can I override the '+=' operator and compile it with static without error?
EDIT:
If I am doing like this without compile static it works fine:
def m1 = new MutableInt(1);
assert (m1 += 1).val == 2 // <----- caution: '+=' not '+' as in previous case
However, if it was inside the method like this:
#groovy.transform.CompileStatic
void compileItWithStatic_2() {
def m1 = new MutableInt(1);
assert (m1 += 1).val == 2
}
The error will be:
Script1.groovy: -1: Access to java.lang.Object#val is forbidden # line -1, column -1.
1 error
P.S. It won't work with static compilation not with dynamic compilation.
The assignment part is throwing the error. A simple + works:
class MutableInt {
int val
MutableInt(int val) { this.val = val }
MutableInt plus(int val) {
return new MutableInt(this.val += val)
}
}
def m1 = new MutableInt(1);
assert (m1 + 1).val == 2;
#groovy.transform.CompileStatic
def compileItWithStatic() {
Map<Long, MutableInt> mutMap = [:].withDefault{ new MutableInt(0) }
mutMap[1L] + 20
mutMap
}
assert compileItWithStatic()[1L].val == 20
Groovy is parsing mutMap[1L] += 20 as mutMap.putAt(1L, 20). This looks like a bug to me. This works: mutMap[1L] = mutMap[1L] + 20, albeit more verbose.
Edit: the second error seems related to the result of the expression (m1 + 1) being parsed as Object. This should work:
#groovy.transform.CompileStatic
void compileItWithStatic_2() {
def m1 = new MutableInt(1) + 1;
assert m1.val == 2
}
I have just started learning Groovy which looks really awesome!
This is very simple example.
"Groovy".each {a -> println a};
It nicely prints as given below.
G
r
o
o
v
y
My question is - 'each' method is not part of String object as per the link below. Then how come it works?
http://beta.groovy-lang.org/docs/latest/html/groovy-jdk/
How can i get the parameters list for a closure of an object?
example String.each has 1 parameter, Map.each has 1 or 2 parameters like entry or key & value.
The relevant code in DefaultGroovyMethods is
public static Iterator iterator(Object o) {
return DefaultTypeTransformation.asCollection(o).iterator();
}
which contains:
else if (value instanceof String) {
return StringGroovyMethods.toList((String) value);
}
String toList is:
public static List<String> toList(String self) {
int size = self.length();
List<String> answer = new ArrayList<String>(size);
for (int i = 0; i < size; i++) {
answer.add(self.substring(i, i + 1));
}
return answer;
}
I want to implement Maybe from Haskell in D, just for the hell of it.
This is what I've got so far, but it's not that great. Any ideas how to improve it?
class Maybe(a = int){ } //problem 1: works only with ints
class Just(alias a) : Maybe!(typeof(a)){ }
class Nothing : Maybe!(){ }
Maybe!int doSomething(in int k){
if(k < 10)
return new Just!3; //problem 2: can't say 'Just!k'
else
return new Nothing;
}
Haskell Maybe definition:
data Maybe a = Nothing | Just a
what if you use this
class Maybe(T){ }
class Just(T) : Maybe!(T){
T t;
this(T t){
this.t = t;
}
}
class Nothing : Maybe!(){ }
Maybe!int doSomething(in int k){
if(k < 10)
return new Just!int(3);
else
return new Nothing;
}
personally I'd use tagged union and structs though (and enforce it's a Just when getting the value)
Look at std.typecons.Nullable. It's not exactly the same as Maybe in Haskell, but it's a type which optionally holds a value of whatever type it's instantiated with. So, effectively, it's like Haskell's Maybe, though syntactically, it's a bit different. The source is here if you want to look at it.
I haven't used the Maybe library, but something like this seems to fit the bill:
import std.stdio;
struct Maybe(T)
{
private {
bool isNothing = true;
T value;
}
void opAssign(T val)
{
isNothing = false;
value = val;
}
void opAssign(Maybe!T val)
{
isNothing = val.isNothing;
value = val.value;
}
T get() #property
{
if (!isNothing)
return value;
else
throw new Exception("This is nothing!");
}
bool hasValue() #property
{
return !isNothing;
}
}
Maybe!int doSomething(in int k)
{
Maybe!int ret;
if (k < 10)
ret = 3;
return ret;
}
void main()
{
auto retVal = doSomething(5);
assert(retVal.hasValue);
writeln(retVal.get);
retVal = doSomething(15);
assert(!retVal.hasValue);
writeln(retVal.hasValue);
}
With some creative operator overloading, the Maybe struct could behave quite naturally. Additionally, I've templated the Maybe struct, so it can be used with any type.
I have a text file, for example : file.txt, I want to read a line, for example line 7, have any way to read directly line 7 without reading other lines? I want to save memory from this work.
Because of the way JME been cut down you cannot do this. You would have to read whole file. Only other way, but may not be well suitable is to read the file , store it in RecordStore new entry per line, but is it really worth...
I think it is possible however you need to use a hashtable which might result to more heap usage.
Anyway, first, the contents of the text file should be stored in a char array. Then, second, the contents of the char array must be moved to the hashtable.
Each line in the text file is separated by a new line. In the char array, the new line (maybe) is translated to '\n'. Concatenate the characters in the array until the new line character is reached. The concatenated characters (minus '\n') will form the string in the first line. There should also be a counter here which should have been initialized to 0 (or 1, whatever you prefer). Save the text to the hashtable; The value will be the string that has been created and the key will be the counter. Increment the counter afterwards. Repeat this process for the remainder of the array until the end of file is reached.
With the hashtable, you can now get the string at line 7 without going through the other lines. Well, basically, each line has been read once. But, at least, you don't have to traverse each line once they have been stored in the hashtable.
Like what I have said earlier, doing this might increase heap usage especially if the text file is very large.
[And, by the way, sorry for the very late response. This is my first time here (I mean I just registered and answered this question) :D ]
Common Code
private String readLine(InputStream _inStream, int lineNum)
throws IOException {
if (null == _inStream) {
throw new IOException("Inputstream null.");
}
if (lineNum < 0) {
return ("Cannot read line a number " + lineNum);
}
final StringBuffer buf = new StringBuffer();
byte c;
int curLine = 1;
while (((c = (byte) _inStream.read()) != -1)) {
//System.out.println((char)c);
if (c == '\n') {
++curLine;
if (curLine > lineNum) {
break;
} else if (curLine < lineNum) {
continue;
}
} else if (curLine != lineNum) {
continue;
}
buf.append((char) c);
}
if (0 == buf.length()) {
return null;
} else {
return buf.toString().trim();
}
}
.
private String readLineWithSkip(InputStream _inStream, long skipCharacters)
throws IOException {
if (null == _inStream) {
throw new IOException("Inputstream null.");
}
if (skipCharacters < 1) {
return ("Cannot skip stream of " + skipCharacters + " characters");
}
final StringBuffer buf = new StringBuffer();
byte c;
_inStream.skip(skipCharacters);
while ((c = (byte) _inStream.read()) != '\n') {
//System.out.println((char)c);
buf.append((char) c);
}
if (0 == buf.length()) {
return null;
} else {
return buf.toString().trim();
}
}
.
InputStream inStream = null;
int fileLength = 39;
int skipCharacters = 10;
int lineNumber = 3;
String myLine = "No line read.";
try {
inStream = Class.class.getResourceAsStream("/test.txt");
if (null != inStream) {
inStream.mark(fileLength);
//For Approach II
myLine = readLine(inStream, lineNumber);
inStream.reset();
//For Approach I
myLine = readLineWithSkip(inStream, skipCharacters);
}
} catch (SecurityException se) {
se.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
inStream.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
inStream = null;
System.out.println(myLine);
}
.
Approach I: Map the line number with no of cumulative characters
Run the file through a code that maps the line number with the last of charater in that line from the 0th position of the file (to be used as skip() value) all +2 ('\r\n\') for each line. You can store this Mapping table either at the start or at the end of the same file.
The run the above common code with method readLineWithSkip(inStream,
skipCharacters); ONLY and judiciously commenting other method calls.
Points to Consider:
Skips to the desired position in the inputstream
Has an overhead of parsing the file and storing the mapping table.
.
Approach II: Read each line till the Nth line is read
The run the above common code with method readLine(inStream,
lineNumber); ONLY and judiciously commenting other method calls.
Points to Consider:
Slow since it has to read each character till it reaches the desired line
No overhead of parsing the file and no storing of the mapping table.
I would like to further simplify the problem of reading the chars without requiring any mapping of characters with line numbers.
...
Form form=new Form("filename");
InputStream fin=fconn.openDataInputStream();
StringBuffer buf =new StringBuffer();
int c;
int counter=-1;
while((c=fin.read())!=-1)
{
counter++;
if(counter==23)
{
form.append(buf.toString());
buf=null;counter=0;
continue;
}
buf.append((char)c);
}
if(counter<23 && counter>=0) // write the skipped chars between the last number read and the end of file
{
form.append(buf.toString());
buf=null;counter=0;
}
fin.close();
...
Hope this would help others.