How to set values of global variable inside inner interface and use the values outside of the interface in adnroid - interface

Is there any way to set value of global variable inside an interface and use the value outside of it?
Here is sample of my code:
class A{
static ArrayList<String> myList = new ArrayList<>();
public static void main(String[] args) {
get(new Inter() {
#Override
public void callBack(ArrayList<String> list) {
myList = list; // here list is the textual data from firebase and I want to use it outside
// this get function call
}
});
myList.toString(); // I want to use it here. But its value out of the function call was null
// but it has the same value with list inside the function call
}
static void get(Inter inter){
// here I want to get some textual data from firebase into ArrayList of String
list = array of String from firebase
inter.callBack(list);
}
interface Inter{
void callBack(ArrayList<String> list);
}
}

You can't use the list directly after the get() call, because the get call is async and has not yet finished.
You can create a method outside the interface that takes the list as a parameter:
public static void main(String[] args) {
get(new Inter() {
#Override
public void callBack(ArrayList<String> list) {
doSomethingWithList(list);
}
});
}
private static void doSomethingWithList(List<Sting> list){
//you code here
}
Or you can use method-reference:
public static void main(String[] args) {
get(<your class>::doSomethingWithList);
}
private static void doSomethingWithList(List<Sting> list){
//you code here
}

Related

Best way to get a param into DoFn's onElement method in Apache Beam

I need to pass a param from the Main to my DoFn method to be used by onElement method. Below is the example in code.
public class MyPTransformClass<T> extends PTransform<PCollection<elemCLass>, PDone> {
#Override
public PDone expand(PCollection<elemClass> input) {
input.apply(ParDo.of(new Fn<>()));
return PDone.in(input.getPipeline());
}
private static class Fn<T> extends DoFn<elemClass, String> {
private Fn() {
}
#Setup
public void onSetup() {
}
#ProcessElement
public void onElement(final ProcessContext context) {
//Do some transformation using a PARAMETER passed from outside
}
}
}
I was wondering how can I pass the PARAMETER above to my DoFn without using side_input. This is not dynamic and will be staying the same throughout the entire execution.
I tried passing it to MyPTransform and then access it from within the DoFn, but it always ends up being Null in DoFn.
Any suggestion is appreciated.
Thank you
Use a property which is passed to the DoFn in the constructor.
If this is a parameter that is available in the pipeline options, then you can read that value in the expand of the PTransform.
private static class Fn extends DoFn<elemClass, String> {
int something;
public Fn(int something) {
this.something = something
}
#Setup
public void onSetup() {
}
#ProcessElement
public void onElement(final ProcessContext context) {
//Do some transformation using a PARAMETER passed from outside
use something
}
}

Why does my ArrayList of objects print out one variable stored inside the object, but not the other

HandDrawn class extends the super-class Car, as you can see. The problem is it doesn't print out the the String name when i try to print the ArrayList that stores the objects. Btw, the ArrayList stores objects of the class Car.
Calm down, if i did something that offends your little feelings with this question, dont down vote... tell me what's up so i know in the future.
public class HandDrawn extends Card {
private boolean niceDrawing;
public HandDrawn(String name, boolean niceDrawing) {
super(name);
this.niceDrawing = niceDrawing;
}
#Override
public String toString() {
return "HandDrawn{" +
"niceDrawing=" + niceDrawing +
'}';
}
public void setNiceDrawing() {
this.niceDrawing = niceDrawing;
}
public boolean getNiceDrawing(boolean niceDrawing) {
return this.niceDrawing;
}
}
public class Main {
static ArrayList<Card> cards = new ArrayList<>();
public static void main(String[] args) {
cards.add(new HandDrawn("Anna", true));
cards.add(new HandDrawn("Kalle", false));
Main myApp = new Main();
myApp.cardList(cards);
}
public void cardList(ArrayList<Card> e) {
for (int i = 0; i <e.size(); i++) {
System.out.println(e.get(i));
}
}
}
This is the Main class and the HandDrawn class
Again, I don't know what is in your super class "Card" but maybe this will help:
In your HandDrawn.java file:
public class HandDrawn extends Card {
private boolean niceDrawing;
public HandDrawn(String name, boolean niceDrawing) {
super(name);
this.niceDrawing = niceDrawing;
}
#Override
public String toString() {
return "HandDrawn{" + "niceDrawing=" + this.niceDrawing + "}";
}
// Notice I have modified the parameters of your getters
// and setters because it looked as if you had swapped them:
public void setNiceDrawing(boolean niceDrawing) {
this.niceDrawing = niceDrawing;
}
public boolean getNiceDrawing() {
return this.niceDrawing;
}
}
In your Main.java file:
public class Main {
static ArrayList<Card> cards = new ArrayList();
public static void main(String[] args) {
// This is the entry point of your program... execution begins here.
cards.add(new HandDrawn("Anna", true));
cards.add(new HandDrawn("Kalle", false));
cardList(cards);
}
// The following two lines are out of place and should not be in class definition:
// Main myApp = new Main();
// myApp.cardList(cards);
// I added static keyword so that it could be used in call above
public static void cardList(ArrayList<Card> e) {
for (int i = 0; i < e.size(); i++) {
System.out.println(e.get(i));
}
}
If you are wanting to use the "Main" class as a piece in another part of your code then probably that means that execution starts somewhere else and never hits the "main" method. If that is the case you will need to call it explicitly (example: Main.main(null);) or reorganize your code to do it in a constructor for the "Main" class. Then your Main myApp = new Main(); and myApp.cardList(cards); lines would make sense in that context and the cardList method would need to be a non-static member method as you had originally written.
EDIT:
I think I misunderstood your formatting... Your code should work as follows:
In your Main.java file:
public class Main {
static ArrayList<Card> cards = new ArrayList();
public static void main(String[] args) {
// This is the entry point of your program... execution begins here.
cards.add(new HandDrawn("Anna", true));
cards.add(new HandDrawn("Kalle", false));
Main myApp = new Main();
myApp.cardList(cards);
}
public void cardList(ArrayList<Card> e) {
for (int i = 0; i < e.size(); i++) {
System.out.println(e.get(i));
}
}
What were you expecting to see? Again I cannot test your code because I don't have "Card" class but when I use the "HandDrawn" class in place of "Card" (without extending "Card") it works as I would expect and prints:
HandDrawn{niceDrawing=true}
HandDrawn{niceDrawing=false}

Method overloading- cant find symbol

public class Overloading {
static void printing() {
System.out.println("Something being printed here");
}
static void printing(String name) {
System.out.println("hello"+name);
}
public static void main(String[] args) {
printing();
printing(rizwana);
}
}
I am trying something to check method overloading. But here is the error I got.
error: cannot find symbol
printing(rizwana);
symbol: variable rizwana
location: class Overloading
rizwana refers to a variable which you have not made in your code. If you want to pass a string, you should pass it as "rizwana".
Call the method as: printing("rizwana");
printing(String name) method will accept a string argument. So you should use printing("rizwana") instead of printing(rizwana). You could this
class Overloading {
static void printing() {
System.out.println("Something being printed here");
}
static void printing(String name) {
System.out.println("hello "+name);
}
public static void main(String[] args) {
printing();
printing("rizwana");
}
}

how to instantiate a java class from within JSNI

I'm trying to instantiate a java type inside a JSNI method body, using the JSO's package-private's ::new() method, but all instances are returned as null handles.
how come?
Is it possible to instantiate the class using a similar syntax to Java?
Here is the relevant code:
public class OnChangeHandlerJso extends JavaScriptObject {
protected OnChangeHandlerJso() {};
public static native JavaScriptObject create() /*-{
return function(cm, changeInfo) {
var eventBus = #com.gigaspaces.codemirror_gwt.client.events.EventUtils::CODE_MIRROR_EVENT_BUS;
var event = #com.gigaspaces.codemirror_gwt.client.events.EditorContentChangeEvent::new()();
// could something like this work?
// var event = new #com.gigaspaces.codemirror_gwt.client.events.EditorContentChangeEvent;
event.#com.gigaspaces.codemirror_gwt.client.events.EditorContentChangeEvent::setCm(Lcom/gigaspaces/codemirror_gwt/client/jsni/CodeMirrorJso;)(cm);
event.#com.gigaspaces.codemirror_gwt.client.events.EditorContentChangeEvent::setChangeInfo(Lcom/gigaspaces/codemirror_gwt/client/jsni/ChangeInfoJso;)(changeInfo);
eventBus.#com.google.gwt.event.shared.EventBus::fireEvent(Lcom/google/gwt/event/shared/GwtEvent;)(event);
};
}-*/;
}
Solved like this:
The overlay type
public class OnChangeHandlerJso extends JavaScriptObject {
protected OnChangeHandlerJso() {};
public static native OnChangeHandlerJso create() /*-{
return function(cm, changeInfo) {
#com.gigaspaces.codemirror_gwt.client.vo.OnChangeHandler::handle(Lcom/gigaspaces/codemirror_gwt/client/jsni/CodeMirrorJso;Lcom/gigaspaces/codemirror_gwt/client/jsni/ChangeInfoJso;)(cm, changeInfo);
};
}-*/;
}
The handler
public class OnChangeHandler implements JsoWrapper<OnChangeHandlerJso> {
public static void handle(CodeMirrorJso cmJso, ChangeInfoJso changeInfoJso) {
// ...
EventUtils.CODE_MIRROR_EVENT_BUS.fireEvent(new EditorContentChangeEvent(cm, changeInfo));
}
}

GWT RPC mechanism how to use non void return type

I have a scenario wherein I need to specify a return type to the Synchrnous function, the code is as follows :
#RemoteServiceRelativePath("show_box")
public interface ShowBoxCommandService extends RemoteService{
public ArrayList<String> showBox();
}
The implementation of the method on the server is :
public ArrayList<String> showBox() {
ArrayList<String> box = new ArrayList<String>();
Iterator<Box> boxes = BoxRegistry.getInstance().getBoxes();
while (boxes.hasNext()) {
box.add(boxes.next().toString());
}
return box;
}
I am trying to define the callback variable in the following format at the client side in order to call the method
AsyncCallback<Void> callback = new AsyncCallback<Void>() {
public void onFailure(Throwable caught) {
// TODO: Do something with errors.
// console was not started properly
}
#Override
public void onSuccess(Void result) {
// TODO Auto-generated method stub
// dialog saying that the console is started succesfully
}
};
update with the aync interface code :
public interface ShowBoxCommandServiceAsync {
void showBox(AsyncCallback<ArrayList<String>> callback);
}
But this is causing the definition of the method in the Async method to change.
Any ideas or clues will be helpful.
Thanks,
Bhavya
P.S. Apologies if this is a repetition
The callback should be:
AsyncCallback<ArrayList<String>> callback = new AsyncCallback<ArrayList<String>>() {
public void onFailure(Throwable caught) {
// TODO: Do something with errors.
// console was not started properly
}
#Override
public void onSuccess(ArrayList<String> result) {
// TODO Auto-generated method stub
// dialog saying that the console is started succesfully
}
};
If you don't need to utilize the result then you can ignore it, but if that is the case, you should probably question your design and why you would need the method to return an ArrayList<String> in the first place.
If the service interface looks like this:
public interface ShowBoxCommandService extends RemoteService {
public ArrayList<String> showBox();
}
then you must have an associated async interface:
public interface ShowBoxCommandServiceAsync {
public void showBox(AsyncCallback<ArrayList<String>> callback);
}
Which means, that the type of the callback that you should pass to showBox is AsyncCallback<ArrayList<String>>.
new AsyncCallback<ArrayList<String>>() {
#Override
public void onSuccess(ArrayList<String> list) {
// ...
}
#Override
public void onFailure(Throwable caught) {
// ...
}
}
Your callback should not be Void. If your synchronous method returns a List of Strings, the async callback method should receive the List. You'll have to use the ArrayList, because the class needs to implement the Serializable interface.
AsyncCallback<ArrayList<String>> callback = new AsyncCallback<ArrayList<String>>() {
public void onFailure(Throwable caught) {
// TODO: Do something with errors.
// console was not started properly
}
#Override
public void onSuccess(ArrayList<String> result) {
// TODO Auto-generated method stub
// dialog saying that the console is started succesfully
}
};
Huh? Your method returns an ArrayList and you are declaring void in your call?
Change <Void> to <ArrayList<String>>