learning...
learning...

Reputation: 3184

Using add or push to add items to an array in groovy

I am getting the following error while push/add items to an array in groovy.

$groovy main.groovy Caught: groovy.lang.MissingMethodException: No signature of method: [LProgressNotes;.push() is applicable for argument types: (ProgressNotes) values: [ProgressNotes@d35dea7] Possible solutions: sum(), plus(java.util.Collection), plus([Ljava.lang.Object;), plus(java.lang.Object), use([Ljava.lang.Object;), plus(java.lang.Iterable) groovy.lang.MissingMethodException: No signature of method: [LProgressNotes;.push() is applicable for argument types: (ProgressNotes) values: [ProgressNotes@d35dea7] Possible solutions: sum(), plus(java.util.Collection), plus([Ljava.lang.Object;), plus(java.lang.Object), use([Ljava.lang.Object;), plus(java.lang.Iterable) at main$_buildOutNotes_closure2.doCall(main.groovy:82) at main.buildOutNotes(main.groovy:75) at main$buildOutNotes.callCurrent(Unknown Source) at main.run(main.groovy:64)

Here is the function:

def buildOutNotes(incomingNotes, systemNotes) {
    ProgressNotes[] outNotes = systemNotes;

    //iterate incoming chares
    incomingNotes.each { incoming -> 
        //split the note further 
        def iNote = splitIncoming(incoming);
        //check that the incoming note is in the system note
        def foundNotes = systemNotes.findAll { it.ProgressNote == iNote.ProgressNote }
        if(!foundNotes){
            //add the incoming note to the outNote 
            outNotes.push(iNote);
        }
    }
    return outNotes;
} 

Here are the articles that show push and add use

https://mrhaki.blogspot.com/2015/01/groovy-goodness-pop-and-push-items-in.html

def list = ['Groovy', 'is', 'great!']
list.push('rocks!')

http://docs.groovy-lang.org/next/html/documentation/working-with-collections.html

def list = [5, 6, 7, 8]
emptyList.add(5)

I am building the example code on https://www.tutorialspoint.com/execute_groovy_online.php.

You can view the example here http://tpcg.io/NGw4szCv

Here is the full code as well:

//package com.javacodegeeks.groovy.date;
//import static java.util.Calendar.*;
//import groovy.json.*;
//import java.util.Properties;
//import java.util.List;

//progress notes object 
class ProgressNotes {
    def ActionDate
    String ActionBy
    String Status
    String ProgressNote

    ProgressNotes(inActionDate, inActionBy, inStatus, inNote){
        this.ActionDate = inActionDate
        this.ActionBy = inActionBy
        this.Status = inStatus
        this.ProgressNote  = inNote
    }
}

//delimiter
String delimiter = "@@";
//out notes
ProgressNotes[] outNotes;
//date patterns
def dateInSystemPattern = "yyyy-MM-dd HH:mm:ss";
def dateIncomingPattern = "MM/dd/yyyy hh:mm ";

/************** SAMPLE DATA START ****************/
//incoming  note string
String incomingNote  = "2019-12-15T01:29:44 User1: December 13 went to pickup the toilet at the wholesaler " + 
                       "then went to site then remove and replace the toilet then  found out that there is a " + 
                       "fruit inside the toilet then clean up the site and silicone around the toilet then " + 
                       "throw the old toilet at dumpster." + delimiter + 
                       "2019-12-13T10:43:05 User2: applied 3 bottles of urinal treatment.  let sit for an " + 
                       "hour. augered out  urinal main. draining excellent.  tried augering toilet. object stuck in " + 
                       "toilet. will not come out. Don will replace." + delimiter + 
                       "2019-12-13T09:18:51 user3: PO 508758 - unclog Washroom " + 
                       "Details: " + 
                       "Unclog toilet bowl and urinal in. Room 116.";

//in system notes
ProgressNotes[] systemNotes = [
    ["2012-01-26T14:52:50", "User1", "DISPATCHED", "reassign to Space Planning to confirm space availability"],
    ["2012-02-01T12:23:05", "User2", "DISPATCHED", "spoke to requestor and she has a few relocations and POD requirements."],
    ["2012-02-01T12:23:45", "User3", "DISPATCHED", "Contacted Customer for clarification spreadsheet is forthcoming for this request."],
    ["2012-02-03T18:45:00", "User1", "DISPATCHED", "Extending date to allow for clean-up of backlog."]
];

/************** SPLIT incomingNote ****************/
def incomingNotes = [];
if (incomingNote != ""){
    incomingNotes = incomingNote.split(delimiter);
}

/************** PICK NOTES ****************/
if (!incomingNotes){
    //No incoming notes push the system notes out
    outNotes = systemNotes;
}
else{
    //check and build the outnotes
    outNotes = buildOutNotes(incomingNotes, systemNotes);
}

println("OUTNOTES Length: " + outNotes.length)
println(" ");

/************** HELPER METHODS ****************/
def buildOutNotes(incomingNotes, systemNotes) {
    ProgressNotes[] outNotes = systemNotes;

    //iterate incoming chares
    incomingNotes.each { incoming -> 
        //split the note further 
        def iNote = splitIncoming(incoming);
        //check that the incoming note is in the system note
        def foundNotes = systemNotes.findAll { it.ProgressNote == iNote.ProgressNote }
        if(!foundNotes){
            //add the incoming note to the outNote 
            outNotes.push(iNote);
        }
    }
    return outNotes;
}

def splitIncoming(incoming){
    //date time characters
    int dateTimeChars = 20;

    def dateAndTimePart = incoming.substring(0,dateTimeChars).trim();
    String remainingNote = incoming.substring(dateTimeChars);
    String userPart = "";
    String notePart = "";
    def remainingNotes = remainingNote.split(":");
    if(remainingNotes){
        userPart = remainingNotes.getAt(0);
        notePart = incoming.substring(dateTimeChars+userPart.length()+1).trim();
    }

    //build the object 
    def outNote = new ProgressNotes(dateAndTimePart, userPart, "", notePart);
    return outNote;
}

Upvotes: 0

Views: 8989

Answers (1)

Szymon Stepniak
Szymon Stepniak

Reputation: 42264

You use an array in your code (ProgressNotes[]), not a list (List<ProgressNotes>). Any of the mentioned methods (add and push) does not exist for Java (and thus Groovy) arrays. An array is fixed size, so once initialized, you can't add any new elements to it - you can only replace existing elements. If you try to add a new element to the array, you will get IndexOutOfBoundsException. Just look at this simple example:

String[] list = ["foo", "bar"]

assert list[0] == "foo"
assert list[1] == "bar"

try {
    list[2] = "new"
} catch (IndexOutOfBoundsException e) {
    println "Caught!"
}

list[1] = "abc"

println list

Output:

Caught!
[foo, abc]

If you want to use List.add() or List.push() (or event groovier leftShift like [] << "elem") you need to use a list instead of an array. Arrays are a good choice if you know the size of the collection is fixed.

//out notes
List<ProgressNotes> outNotes;

Upvotes: 2

Related Questions