Someone please explain contractions in Java code syntax [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
A question from dummy.... What is the full version of code below?
How to interpret it in classic (long) version of code?
FirebaseDatabase.getInstance()
.getReference()
.push()
.setValue(new ChatMessage(input.getText().toString(),
FirebaseAuth.getInstance()
.getCurrentUser()
.getDisplayName())
);

The called methods all return an object (apart of the last one), not void. In some other languages, you'd call them functions as opposed to procedures.
Since the method returns an object, you can in turn call a method on that returned object, and chain the calls like this.
It's equivalent to something like this, if that makes it more clear to you:
<some class> var1 = FirebaseDatabase.getInstance();
<some class> var2 = var1.getReference();
<some class> var3 = var2.push();
var3.setValue(new ChatMessage(input.getText().toString(),
FirebaseAuth.getInstance()
.getCurrentUser()
.getDisplayName())
);

It's just Java
SomeClass1 instance = FirebaseDatabase.getInstance();
SomeClass2 reference = instance.getReference();
SomeClass3 push = reference.push();
SomeClass4 authInstance = FirebaseAuth.getInstance();
SomeClass5 currentUser = authInstance.getCurrentUser();
SomeClass6 displayName = currentUser.getDisplayName();
SomeClass6 message = input.getText();
SomeClass7 messageAsString = message.toString();
SomeClass8 chatMessage = new ChatMessage(messageAsString, displayName);
push.setValue(chatMessage);
Note: Code is ridiculously formatted for the purpose of clarity. Please don't use formatting like this example in your code.

Related

JAVA: Initialize Object Class [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm new learner in programming, and i have a logic problem.
I would like to initialize a object class to settle it.
I have 2 Entities object Class:
Entity1:
private Date date;
private List<Entity2> entity2;
.... Getters and Setters ....
Entity2:
private String description;
.... Getters and Setters ....
Now I've init the entities
Entity1 entity1 = new Entity1();
List<Entity2> Entity2 = new ArrayList<Entity2>();
entity1.setEntity2(entity2);
I have no error message by doing this, in debugging mode I see in the entity1 and the ArrayList of entity2, but empty. I see no object inside, just this [ ] and I would like to see "description" object (which will be normal), like this [description = null];
Someone can explain to me what I do wrong ?
Thanks so much for your help
You're really close. Let's consider what you've got thus far.
Entity1 entity1 = new Entity1();
You've created yourself an instance of Entity1. So far, this looks like this:
{
Date: null,
entity2: null
}
Then you've issued the following commands:
List<Entity2> Entity2 = new ArrayList<Entity2>();
entity1.setEntity2(entity2);
So now your entity1 variable looks like this:
{
Date: null,
entity2: [],
}
The next thing you need to do is add a new object into your new list. First, you need to make yourself an instance of Entity2.
Entity2 myEntity = new Entity2();
Then you need to put it into your list, using the add command on the List interface. I won't write this out for you, you need to work it out yourself. Have fun!

easier way to format lines of code in Java to make your job easier? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am currently making an application that uses an api and it prints out information about that thing. So basically it gets the api and If i do System.out.println(result.getPlayer().get("displayname")); it will return the display name of the player that I am searching for. I was wondering if there was a way to make result.getPlayer().get("displayname") a variable because I have hundreds of statistics that I need to gather. so is it possible to have that line of code called displayname? Sorry if you don't understand.
I suggest that you make a special statistics/logging class that has static methods specifically for this. For example with your case, the following class can be used both to get the name and to print it. Of course you can combine them into a single method if you want just one functionality.
public class StatsLog {
public static String getPlayerDisplayName(final Result result) {
return (result == null)
? null
: result.getPlayer().get("displayname");
}
public static void printPlayerDisplayName(final Result result) {
final String displayName = getPlayerDisplayName(result);
if (displayName != null) {
System.out.println(displayName);
}
}
}
And when you call it:
StatsLog.printPlayerDisplayName(result);
You can use a getter like #Andrew Tobilko said. like this:
public String getDisplayName(){
return (result != null)? result.getPlayer().get("displayname") : "";
}
However, it depends on what is the "result" and the design of your class. And make sure to check for Null.

Looping through Object of Objects in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have an object which in turn contains other objects. Now I have to iterate through this main object and then pick each object and then iterate through them to find out whether any empty fields are present in them. If the object itself is empty, I have to cut it out of main object. Any thoughts on this please.
public class Transactions {
private Integer totalTransactionCount = null;
private List<Transaction> transactionsList = new ArrayList<Transaction>();
}
public class Transaction {
private String amount = null;
private Foreign foreign = null;
}
public class Foreign {
private String amount = null;
private String commissionAmount = null;
private String exchangeRate = null;
}
Now I have a Transaction object with me and I have to loop throught each of its fields and in turn loop through their fields to find out any null/empty fields.
pseudo code for looping through a list of lists:
for each (innerList in outerList) do
if(innerlist.size == 0) then
//Code for removing empty inner lists.
else
for each ( object in innerList) do
//Check if objects are empty as well and remove it
end for
end if
end for
EDIT: Pointing out lack of research.
I would like to point out that you haven't really done your research properly, simply by googling iterate list of object as well as iterate list of list of object I got plenty of solutions.
Not to mention a question already asked here on Stack Overflow, please read the first answer of this post

rewrite java code to groovy code [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have this code in Java.
MyObject a = MyObject.newMyObject();
a.setParameter(Parameters.BASIC);
CompositeObject co = CompositeObject.newCompositeObject();
co.add(BasicFactory.newInputStream(new FileInputStream(file), ResourceType.BASIC, a);
Is it possible to do it better using Groovy?
Here's a start.
def a = MyObject.newMyObject()
a.parameter = Parameters.BASIC
def co = CompositeObject.newCompositeObject()
co.add(BasicFactory.newInputStream(new FileInputStream(file), ResourceType.BASIC, a))
If all you really need is the co then you can do something like this to limit the scope of temporary variables.
def co = CompositeObject.newCompositeObject().with {
def a = MyObject.newMyObject()
a.parameter = Parameters.BASIC
def factory = BasicFactory.newInputStream(new FileInputStream(file), ResourceType.BASIC, a)
co.add factory
return it
}

Need bindings equivalent of String.replaceAll(...) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to create a binding that allows me to do the equivalent of String.replaceAll(...) but with bindings. I have a string, "${driver} driving ${name}", and I want the keys, "${driver}", etc. to be replaced with the specific property. I also want the returned property to be able to add listeners so when driverProperty or another changes, the returned property value will change without having to re-call getString().
public String getString(Derby derby) {
String ret;
if (driverProperty.get().equals("") && nameProperty.get().equals("") && numberProperty.get().equals("") && groupProperty.get().equals("")) {
ret = "[blank]";
} else {
ret = (String) derby.getSettings().get("general.cardisplay").getValue();
ret = ret.replace("${driver}", driverProperty.get());
ret = ret.replace("${name}", nameProperty.get());
ret = ret.replace("${number}", numberProperty.get());
ret = ret.replace("${group}", groupProperty.get());
}
return ret;
}
Use Bindings.createStringBinding(). This takes a function supplying the String and a list of values to observe; if any of those values change the binding is marked as invalid.
Your question isn't too clear to me, but I think you can do something like
StringBinding formattedString = Bindings.createStringBinding(
() -> getString(derby),
derby.settingsProperty(),
nameProperty, numberProperty,
driverProperty, groupProperty);
Now you can do things like
formattedString.addListener((obs, oldFormattedString, newFormattedString) -> {
// invoked any time the formatted string changes...
});
or
Label label = new Label();
label.textProperty().bind(formattedString);

Categories