Decorate existing class to support javafx properties - java

I have an existing class, that I want to keep ignorant of javafx. Is there a generally accepted way to decorate or adapt it so that it supports javafx properties?
I want to do something like the following (which is obviously wrong):
public class FooWrapper {
/**
* Decorates a Foo instance with javafx stuff
*/
private final Foo foo;
public FooWrapper(Foo toWrap) {
this.foo = toWrap;
}
private final StringProperty name = new SimpleStringProperty();
public final StringProperty nameProperty() {
return this.foo.getName();//?????
}
public final String getName() {
return this.nameProperty().get();
}
public final void setFirstName(final String name) {
this.nameProperty().set(name);
}
}
public class Foo {
/**
* Basic class I want to keep ignorant of javafx
*/
private String name = "hello";
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}

Use the classes in the javafx.beans.property.adapter package.
public class Foo {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class FooAdapter {
private final JavaBeanStringProperty name;
public FooAdapter(Foo foo) {
try {
name = JavaBeanStringPropertyBuilder.create().bean(foo).name("name").build();
} catch (NoSuchMethodException ex) {
throw new RuntimeException(ex);
}
}
public final void setName(String name) {
this.name.set(name);
}
public final String getName() {
return name.get();
}
public final StringProperty nameProperty() {
return name;
}
}
The adapter property, as created above, requires that the underlying object follows the Java Bean convention for properties. However, there are ways to customize what methods to use for getters/setters.
The adapter property will get the value from the underlying object and, if writable, also write to the underlying object when updated. It can also observe the underlying object for changes if it supports PropertyChangeListeners. Note that this functionality is implemented using reflection; if you are using modules you need to add the appropriate exports/opens directives to your module-info (see the javadoc of the various properties, such as JavaBeanStringProperty, for details).

Related

Get all objects with the same constructor as integer?

I got the following class:
public class Possibility {
private String name;
public Possibility(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
If I now have many classes that extend "Possibility", how can I find how many instances exist of classes that extend Possibility?
You can use a static field as a counter in Possibility class and use it to increment as the objects are created. This is more efficient and secure than using reflection.
package so;
public class Possibility {
private static int counter = 0;
private String name;
public Possibility(String name) {
counter += 1;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String[] args) {
Possibility1 p1 = new Possibility1("p1");
Possibility2 p2 = new Possibility2("p2");
System.out.println(Possibility.counter);
}
}
Possibility1
package so;
public class Possibility1 extends Possibility {
public Possibility1(String name) {
super(name);
}
}
Possibility2:
package so;
public class Possibility2 extends Possibility {
public Possibility2(String name) {
super(name);
}
}
Possibility3
package so;
public class Possibility3 extends Possibility {
public Possibility3(String name) {
super(name);
}
}
The Reflections library provides a pretty easy way to do this:
int numSubTypes = reflections.getSubTypesOf(Possibility.class).size();
you must create an integer attribute in Possibility class and and you can get this integer from another class that extends from Possibility, like this:
class Possibility{
public int someInteger;
//getter
public int getSomeInteger(){
return this.someInteger;
}
}
class someClass extends Possibility{
public void someMethode(){
Possibility possibility = new Possibility("someName");
//get someInteger
possibility.getSomeInteger();
}
}

Deserialize an object's property which has an inconsistent name?

Using Retrofit here to consume Google Civic API.
The library requires you to create a model of what the API will return as I have done already with Election. Which is basically a copy of the google documentation.
(Retrofit binds the response properties to properties with the same name)
Election.Java :
public class Election {
private long id;
private String name;
private String electionDay;
private String ocdDivisionId;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getElectionDay() {
return electionDay;
}
public void setElectionDay(String electionDay) {
this.electionDay = electionDay;
}
public String getOcdDivisionId() {
return ocdDivisionId;
}
public void setOcdDivisionId(String ocdDivisionId) {
this.ocdDivisionId = ocdDivisionId;
}
}
But Representatives have an inconsistent property name, thus I don't see a way to model this in a way Retrofit will know how to deserialize the API's response.
Representatives object (JSON) :
property name is called (key)
How do I let Retrofit deserialize a model that captures the property named variable after a key of the division?
Assuming you're using a Gson converter, I personally would use a map. I guess the same can be achieved with other converters, but I never used them. Say you have the following object:
public class Division {
#SerializedName("name")
#Expose
private String name;
#SerializedName("alsoKnownAs")
#Expose
private List<String> alsoKnownAs = new ArrayList<>();
#SerializedName("officeIndices")
#Expose
private List<Integer> officeIndices = new ArrayList<>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getAlsoKnownAs() {
return alsoKnownAs;
}
public void setAlsoKnownAs(List<String> alsoKnownAs) {
this.alsoKnownAs = alsoKnownAs;
}
public List<Integer> getOfficeIndices() {
return officeIndices;
}
public void setOfficeIndices(List<Integer> officeIndices) {
this.officeIndices = officeIndices;
}
}
Which represents the object inside the divisions array. You can then have the class:
private class Divisions {
#SerializedName("divisions")
#Expose
private Map<String, Division> divisions = new HashMap<>();
// ...
}
Notice the usage of a map here? Behind the scenes Gson will be able to serialise and deserialise your objects. The class Divisions is the root of the json you gave us in the question.
Hope this helps

Can a static final field be initialized in subclasses? If so, how?

I need to write some code which is as follows:
public class Person {
public static final String NAME;
public Person(String NAME) {
this.NAME = NAME;
}
}
public class Player extends Person {
public Peter(String name) {
super(name);
}
}
It's basically, I want the Player class to have a static final field called NAME, that is being initialized somewhere else, without manually writing in every class public static final String NAME = "Peter".
Is it possible?
As it has been said in the comments, you have poorly declared your NAME variable. In actuality, you don't want it to be static (although you can keep the final modifier, if you want). Your code should, instead, be something along the lines of:
public class Person {
public final String name;
public Person(String name) {
this.name = name;
}
}
public class Player extends Person {
public Player(String name) {
super(name);
}
}
Every person should have their own name; you don't want all objects to be sharing one NAME field
I do not know if I fully understand your question, but I think you have a few mistakes in your code. Like declare name of person as static variable, because static variables are often used as variables for the entire class, and if you changed the name, would change the name to the entire class, not for one instance. Also final is wrong, because you cannot set final variable.
I would do something like this:
public class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Override
public String toString() {
return String.format("Person: %s", this.getName());
}
}
public class Player extends Person{
public Player(String name) {
super(name);
}
public String toString(){
return String.format("Player: %s", this.getName());
}
}
public class Match {
private Player player_one;
private Player player_two;
public Match(Player player_one, Player player_two) {
this.player_one = player_one;
this.player_two = player_two;
}
public Player getPlayer_one() {
return player_one;
}
public void setPlayer_one(Player player_one) {
this.player_one = player_one;
}
public Player getPlayer_two() {
return player_two;
}
public void setPlayer_two(Player player_two) {
this.player_two = player_two;
}
#Override
public String toString() {
return String.format("Right now are playing %s VS %s",player_one.getName(), player_two.getName());
}
}
public class PlayerTest {
public static void main(String[] args) {
Player peter = new Player("Peter");
Player anna = new Player("Anna");
Match tennisMatch = new Match(peter, anna);
System.out.println(tennisMatch.toString());
}
}
I static field (variable) only exists once for all instances of your class. Therefore what you try does not work by design.
What value would you expect the field to have after you created three different instances of this class using different parameters?
A final variable cannot be changed once it got initialized. For static variables this happens before the first instance of the class is even constructed. At the moment the constructor is executed the field cannot be changed anymore.
To initialize a static final variable you have to assign a value directly at the definition using the = operator or you have to do it in a static initializer which looks like this:
public class FooBar {
public static final String STATIC_VARIABLE;
static {
STATIC_VARIABLE = "Hello World";
}
}
You can make it like this:
private static final NAME;
public Player(String name){
NAME = name;
}
A final varible can be initialized once only if it wasn't initialized yet.
So in this way the constructor is helping you make it.

Reasoning behind this and scope (I think related to scope)

I'm coming to Java from Python and thought that this is basically like Python's self...but this small code confuses me. Functionally, this code:
public class Test {
private String name;
public Test(String givenName)
{
this.name = givenName;
}
public String nameGet()
{
return this.name;
}
public static void main(String[] args)
{
Test example = new Test("Hello Guys");
System.out.println(example.nameGet());
}
}
does the same exact thing as this code:
public class Test {
private String name;
public Test(String givenName)
{
name = givenName;
}
public String nameGet()
{
return name;
}
public static void main(String[] args)
{
Test example = new Test("Hello Guys");
System.out.println(example.nameGet());
}
}
Since this, pardon the pun, seems to be the case, what then is the point of referring to this when working within the class?
public Test(String givenName)
{
this.name = givenName;
}
The this. is not needed in this case or in the get method). It is commonly used when the code is like this instead:
public Test(String name)
{
this.name = name;
}
Which tells the compiler to set the instance variable (this.name) to the local variable (name).
Some people do it to be very clear that they are using an instance variable.
It's often not needed but may be necessary in case of ambiguity.
Say your constructor parameter was called name then there would be no way of determining which variable you're referring to.
Thus you would have to use:
public class Test {
private String name;
public Test(String name) {
this.name = name;
}
}
(On a side note; if you'll ever work with inner classes and you've got name ambiguity you use OuterClass.this:
public class Test {
private String name;
private class InnerTest {
InnerTest(String name) {
Test.this.name = name;
}
}
public Test(String name) {
new InnerTest(name);
}
}

Printing the name of an object in Java

I have researched, and although this is a really simple issue, I am not sure how to solve it.
The code I have looks like this:
public class Playlist {
public Playlist(String name) {
}
}
Separate files of course:
#Test
public void CreatePlaylist(){
Playlist myPlaylist = new Playlist("Workout Playlist");
}
I am trying to print the actual name of this new playlist "workout playlist" but I can't seem to find a way to do so.
You need to store the name of your playlist in an instance variable. For instance:
public class Playlist {
private final String name;
public Playlist(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Then you can print it with:
System.out.println(myPlayList.getName());
If you want to make the name mutable, then get rid of the final modifier and add a setName(String) method.
write get method to name or override toString method in the class
public class Playlist {
private String name;
public Playlist (String name) {
this.name = name;
}
public String getName() {
return name;
}
#Override
public String toString() {
return "Playlist [name=" + name + "]";
}
}
Print the name using
System.out.println(playlistObject.getName());
or
System.out.println(playlistObject).
I would prefer setting a getter method over toString() though.
public class Playlist {
private String name;
public Playlist(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Then to show the name:
#Test
public void CreatePlaylist(){
Playlist myPlaylist = new Playlist("Workout Playlist");
System.out.println(myPlaylist.getName());
}
You are not at all storing the 'name' property in your object. So obviously you can't access name. One way is
public class Playlist {
public String name;
public Playlist(String name) {
this.name = name;
}
}
Now you should be able to access your attribute from your testcase like this.
#Test
public void CreatePlaylist(){
Playlist myPlaylist = new Playlist("Workout Playlist");
System.out.println(myPlaylist.name);
}

Categories