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);
Related
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 1 year ago.
Improve this question
I'm not sure if I'm overthinking this but should I do this
if (!searchList.isEmpty()) {
String search = searchList.get(0).getText();
return List.of(search.split("\n"));
} else {
return null;
}
or should I do this
if (!searchList.isEmpty()) {
String search = searchList.get(0).getText();
return List.of(search.split("\n"));
}
return null;
Some advice :
You should never return a null value. It is a bad practice
You should test for true instead of false. It makes your code more readeable
Your should look like this :
if (searchList.isEmpty()) {
return Collections.emptyList();
}
String search = searchList.get(0).getText();
return List.of(search.split("\n"));
Neither.
if (searchList.isEmpty()) {
return null;
}
String search = searchList.get(0).getText();
return List.of(search.split("\n"));
I didn't like the negative condition ("if not empty") when it wasn't really needed.
The way I think of what I wrote, it's "get rid of the edge case first, then deal with the main logic".
This is of course mere opinion. Eventually you'll develop your own taste for how to lay out code; and good taste is one of the more important attributes of a programmer.
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 2 years ago.
Improve this question
I have this code:
entity.conditions().forEach(entityCondition - > {
if (entityCondition instanceof LinkCondition) {
LinkCondition condition = (LinkCondition) entityCondition;
} else if (entityCondition instanceof OppositeLinkCondition) {
//...
} else if (entityCondition instanceof PropertyEqualCondition) {
//...
} else if (entityCondition instanceof PropertyLocalTimeRangeCondition) {
//...
}
// and 20 or so, more conditions...
});
In which I am looking for an alternative than IF-ELSE statement, a more elegant approach in dealing with dozens of instanceof conditions.
Unfortunately, there is no such syntax in Java yet. You can try Kotlin which already supports this:
val b = when (a) {
is String -> a.length
is Int -> a
is Long -> (a and 0xFFFFFFFF).toInt()
else -> a.hashCode()
}
As #Progman pointed out, this question is similar to other questions, you should check them out too.
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 5 years ago.
Improve this question
I have this piece of code:
Name name = address.getName();
if (null != name && null != name.getFullName()) {
addressInfo.setName(name.getFullName());
} else {
addressInfo.setName(StringUtils.EMPTY);
}
Email emailProfile = address.getEmailAddresses();
if (null != emailProfile && emailProfile.hasPrimaryEmail()) {
addressInfo.setEmail(emailProfile.getPrimaryEmail().getEmailAddress());
} else {
addressInfo.setEmail(StringUtils.EMPTY);
}
VoicePhone voicePhoneProfile = address.getVoicePhones();
if (null != voicePhoneProfile && voicePhoneProfile.hasPrimaryPhone()) {
addressInfo.setPhoneNumber(voicePhoneProfile.getPrimaryPhone().getPhoneNumber());
} else {
addressInfo.setPhoneNumber(StringUtils.EMPTY);
}
They are basically doing the same thing but the getter and setter behavior is different.
Is there a way to write one single generic method to make these 3 block of code into 3 lines of code?
I would discourage trying to meld these methods together. There's a simpler win that you can get here, actually.
Take a closer look at what they're doing:
setName only sets the name to the full name if it exists, otherwise it defaults to "empty".
setEmail only sets the email address if it exists, otherwise it defaults to "empty".
setPhoneNumber only sets the phone number if a primary phone number exists, otherwise it defaults to "empty".
Enforce the defaults in the bean itself as opposed to the conditional logic here.
public class Name {
private String name = StringUtils.EMPTY;
}
// and so forth for Email and VoicePhone
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.
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 8 years ago.
Improve this question
im creating minecraft sectors system..
I want to get worldguard region what player is actually.
All regions are saved into files like (Regions/region1.yml, region2.yml)
Now my question is:
How can i list all files in folder regions to String list?
i need it to do something like this
if(e.getregion.getiid.contains(list1) {
//do something
}
you can use list function of File class http://docs.oracle.com/javase/7/docs/api/java/io/File.html#list()
File regionFolder = new File("path/to/Regions/folder");
String[] regionFile = regionFolder.list();
If you want to leverage the capabilities of ArrayList to check if a String is in the list, you can do this:
File regionFolder = new File("/path/to/files");
FileFilter filter = new FileFilter() {
#Override
public boolean accept(File pathname) {
return pathname.getPath().endsWith(".yml");
}
};
ArrayList<String> myYmlFiles = new ArrayList<String>(Arrays.asList(regionFolder.list(filter)));
You can then use .contains on the myYmlFiles object, and it will only contain your .yml files.
if ( myYmlFiles.contains(e.getRegion().getId()) ) {
// do something
}