This question already has answers here:
How to serialize a lambda?
(5 answers)
Closed 6 years ago.
I am trying to create a class which models a program stack. Is it possible to make this class serializable? I want to be able to use this as Akka messages. Thanks!
public class ProgramStack<T>{
public final Queue<UnaryOperator<T>> programStack;
private T context;
ProgramStack(Queue<UnaryOperator<T>> programStack, T context) {
this.programStack = programStack;
this.context = context;
}
public void next() {
UnaryOperator function = programStack.poll();
function.apply(context);
}
public boolean hasNext() {
return !programStack.isEmpty();
}
}
For one you don't serialize methods you serialize objects that have behaviors. Anyway in order to be able to serialize this you must implement Serializable. All of it's member must do this. If you look at the doc https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html Queue does not implement Serializable so make sure what ever object implements Queue is serializable. I don't know what T is must but that must also implement Serializable in order to serialize your object of type ProgramStack.
import java.io.Serializable;
public class ProgramStack<T> implements Serializable {
// etc etc
}
Related
This question already has answers here:
Deserializing polymorphic types with Jackson based on the presence of a unique property
(7 answers)
Closed 3 years ago.
I have the following json:
[
{
"dog":{
"noise":"bark",
"dogfood":"something"
}
},
{
"cat":{
"noise":"meow",
"catfood":"iams"
}
}
]
and the following models:
public abstract class Animal {
}
public class Dog {
private String noise;
private String dogFood;
}
public class Cat {
private String noise;
private String catFood;
}
How do I deserialize each item in the json array with inheritance based on whether the root name for each element is dog or cat?
At the Github page of Jackson someone asked for a solution for the the same problem as what you are facing, see here: https://github.com/FasterXML/jackson-databind/issues/1627
A work around for the time-being would be a custom deserializer where a field within the class should contain what type of class it is so it can be properly deserialized. See here for the work around: https://stackoverflow.com/a/50013090/6777695
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
I have a use-case like this:
Based on the parameter passed - I have to create an object corresponding to it but the underlying functionality remains same.
public void selectType ()
{
String type = "ABC";
publishType(type);
}
public void publishType(String type)
{
if (type.equals("ABC"))
ABCtype publishObject = new ABCtype();
if (type.equals("XYZ"))
XYZtype publishObject = new XYZtype();
publishObject.setfunctionality();
}
What is a better way to approach this?
Which design pattern does it fall in?
Another doubt I have is - how to initialize publishObject?
It gives an error like this.
but the underlying functionality remains same
you maybe consider design suing interfaces..
Do some nice Archi- Design like:
define an interface, and 2 classes that implement the interface, then
declare an object foo and initialize it according to the parameter..
Example:
interface IObject{
//methods here
}
class A implements IObject{}
class B implements IObject{}
public void selectType ()
{
IObject foo = getObject(1);
}
public IObject getObject(int type){
if (type ==1){
return new A();
}else{
return new B();
}
}
This question already has answers here:
Java collections covariance problem
(3 answers)
Closed 6 years ago.
I'm trying to write a general method that writes different types of Java Beans (so List<JavaBean>) to a file. I'm currently constructing a FileManager utility class. Each Java Bean implements the same interface. Here's an example of what I'm trying to do.
public interface Data { method declarations }
public class RecipeData implements Data { class stuff goes here }
public class DemographicData implements Data { class stuff goes here }
final public class FileManager {
public static void writeToCsvFile(String filename, List<Data> data) { file writing logic goes here }
}
I want to be able to pass a List<RecipeData> and a List<DemographicData> to this method. Obviously what I have does not work.
It doesn't seem I can even do the following:
List<Data> data = new ArrayList<RecipeData>();
How would this normally be done? In Swift I might use the as? keyword to cast it to the correct type.
**************EDIT**************
Just to preface I'm using the SuperCSV library to assist in parsing rows into a Java Bean and I am using the accepted answer below for the method definition. So I have the following code:
Data dataset;
while((dataset = beanReader.read(Data.class, nameMappings, processors)) != null ) {
container.add(dataset);
}
I get the following error:
The method add(capture#1-of ? extends Data) in the type List is not applicable for the arguments (Data)
dataset needs to be either type RecipeData or DemographicData for this to work I'd assume. Is there an easy way to fix this so that it is flexible if I add more Beans in the future?
final public class FileManager {
public static void writeToCsvFile(String filename, List<? extends Data> data) { file writing logic goes here }
}
additionally, you can declare
List<Data> data = new ArrayList<RecipeData>();
as
List<Data> data = new ArrayList<Data>();
or, in Java 7,
List<Data> data = new ArrayList<>();
and just populate it with RecipeData, since either way you are losing the information that this List is to contain only RecipeData
This question already has answers here:
What is Type<Type> called?
(3 answers)
Closed 7 years ago.
I am working on a Java application in which I found this class:
public class TipologiaGenerica<K> {
private K codice;
private String descrizione;
public TipologiaGenerica(K codice, String descrizione) {
this.codice = codice;
this.descrizione = descrizione;
}
public K getCodice() {
return codice;
}
public void setCodice(K codice) {
this.codice = codice;
}
public String getDescrizione() {
return descrizione;
}
public void setDescrizione(String descrizione) {
this.descrizione = descrizione;
}
}
As you can see this class is declared as: TipologiaGenerica and K seems to be something like an object type that could be passed when a specific TipologiaGenerica object is created and that determinate the type of one of its inner field, this one:
private K codice;
Infact, somewhere else in the code, I find a TipologiaGenerica object creation:
TipologiaGenerica<String> dataPerLista = new TipologiaGenerica<String>(dataString, dataString);
What exatly mean? I think that doing in this way it is creating a specific TipologiaGenerica object having the inner codice field that is a String.
Is it my reasoning correct? What is the name of this specific use of Java? What are the most common purpose of this type of constructor?
It is called Generic Types. You can use them to generalize some classes / methods into typesafe code "templates".
Check the Oracle's tutorial regarding this topic
https://docs.oracle.com/javase/tutorial/java/generics/
Is it my reasoning correct?
yes.
What is the name of this specific use of Java?
Generics
What are the most common purpose of this type of constructor?
type safety
This is called generics in Java. The use of this type of programming is to ensure type safety and that you could reuse the same parent class by inserting various object types. for e.g. in your case, you have made
TipologiaGenerica<String>
Users can reuse the same class for other types, for e.g.
TipologiaGenerica<Integer>
This question already has an answer here:
Provide parameter SchemaType for XmlObject
(1 answer)
Closed 6 years ago.
I'm having a class like
public class CreditCardResponseDocumentImpl extends org.apache.xmlbeans.impl.values.XmlComplexContentImpl implements creditcard.CreditCardResponseDocument
{
public CreditCardResponseDocumentImpl(org.apache.xmlbeans.SchemaType sType)
{
super(sType);
}
}
While calling the constructor in a groovy file like
CreditCardResponseDocumentImpl creditCardResponseDocument = new CreditCardResponseDocumentImpl(<What parameter should I pass here>).
It's basically asking for (SchemaType sType)
Any help would be greatly appreciated.
Thanks,
Nizam
If yo are created parametrized constructor you have to create default constructor also.
public class CreditCardResponseDocumentImpl extends org.apache.xmlbeans.impl.values.XmlComplexContentImpl implements creditcard.CreditCardResponseDocument
{
public CreditCardResponseDocumentImpl(){
}
public CreditCardResponseDocumentImpl(org.apache.xmlbeans.SchemaType sType)
{
super(sType);
}
}
If you are not calling your defaulat constructor then you have to pass SchemaType sType
Then you need to provide it a SchemaType, here is the javadoc for it
every XML Bean instance has an actual SchemaType, obtainable by
XmlObject#schemaType
I guess your CreditCardResponseDocumentImpl is an XmlObject