I recently upgraded my project from Java 8 to Java 10 and had a LOT of warnings that Observer and Observable were deprecated now. The suggestion with which to replace them was the Flow API - The problem is I have yet to find a decent tutorial on how to code for the Flow API. Everything I've found says to not implement the Publisher/Subscriber directly and also much of what I've found suggests that the Flow API is heavily based on multithreading.
Note: I've sought this answer out already on StackOverflow as well as Quora and a few other sites and several people cite that Observable objects can't be serialized which is flatly untrue as I implement Serializable for every one of my beans and many of my other classes as well and have even serialized to file a few of my objects and read them back from the file back into object form.
However, the multithreading thing is an issue - I don't WANT to use streams or multithreading when responding to changes to a model object. Question 1 is - Is that an absolute requirement to using the Flow API or is there another set of classes/interfaces that don't require multithreading and streams?
When looking at the javadocs for Flow.publisher thinking that would be what I would replace Observable with - Because it's an interface, that means I guess that I'd have to put (similar to my ListModel and TableModel class implementations) a collection of subscribers(similar to Observers??) and put custom code to add and remove them from my publisher? Question 2 - Do I have that right?
Question 3.) What is the generic type argument in Flow.Publisher and Flow.Subscriber? It shows a type but I have no idea where that type is used - I'll put code here for one of my model classes:
package net.draconia.frenchstudy.model;
import java.io.Serializable;
import java.util.concurrent.Flow.Publisher;
import java.util.concurrent.Flow.Subscriber;
public class Category implements Publisher<Category>, Serializable
{
private static final long serialVersionUID = 5281400324476454101L;
public static final Category EMPTY_CATEGORY = new Category();
private Integer miId;
private String msCategory;
public Category()
{ }
public Category(final int iId)
{
setId(iId);
}
public Category(final String sCategory)
{
setCategory(sCategory);
}
public Category(final int iId, final String sCategory)
{
setId(iId);
setCategory(sCategory);
}
public String getCategory()
{
if(msCategory == null)
msCategory = "";
return(msCategory);
}
public int getId()
{
if((miId == null) || (miId < 0))
miId = 0;
return(miId);
}
public void setCategory(final String sCategory)
{
if(sCategory == null)
msCategory = "";
else
msCategory = sCategory;
setChanged();
notifyObservers();
}
public void setId(final Integer iId)
{
if((iId == null) || (iId < 0))
miId = 0;
else
miId = iId;
setChanged();
notifyObservers();
}
public void subscribe(final Subscriber<? super Category> subscriber)
{
}
public String toString()
{
return(getCategory());
}
}
It's partially bastardized as I was trying to replace Observable with Flow.Publisher but have no idea what to put in the subscribe method code. I took a wild guess at the generic type as the same type as the class on which being worked but I am not sure if that's correct.
I've always been fine with putting logic in the various Observers to handle just 1 possible change in the Observable object and leave other changes to other Observers - The order of the execution of said observers to me never mattered because I coded with that in mind and it wouldn't rely on one executing before or after another. I've never noticed any reduction of execution speed due to Observers executing more than once in many cases so this seems foolish of a decision on Oracle's part but I still want to know - How do I use the Flow API to replace the Observable/Observer code in my program? Or if it can't function like that, then what can I use because I've seen a possible solution to use PropertyChangeListeners but my model objects are NOT properties and I don't want to define properties (Never used PropertyChangeListener so I'm not sure how they work either) given that in the dozens of model objects that would surmount to hundreds if not close to a thousand properties to define if I had to define them. Any links to tutorials, code, or examples to follow will be helpful.