-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi,
I have some classes (wrappers around a string, each with their own
semantics) which can only be created through a factory method, which
ensures that there is always only one object of the given class with the
given name (String). I want to make these classes Serializable. What
is the best way to do this?
Example class:
import java.util.HashMap;
import java.util.Map;
public class FirstOrderVariable {
private static final long serialVersionUID = -6957388990994668892L;
/**
* Factory method to get a variable. If a variable with the given name
already
* exists, it is returned.
*
* @param name
* The name of the variable.
* @return The unique variable with the given name. If the name is not
* effective, a variable with a random name is returned. | if
( name
* != null ) | then result.getName() == name
*/
public static FirstOrderVariable getVariable(final String name) {
FirstOrderVariable result;
if (name == null) {
result = new FirstOrderVariable();
} else {
result = FirstOrderVariable.variables.get(name);
}
if (result == null) {
result = new FirstOrderVariable(name);
FirstOrderVariable.variables.put(name, result);
}
return result;
}
private static Map<String, FirstOrderVariable> variables = new
HashMap<String, FirstOrderVariable>();
private final String name;
private FirstOrderVariable() {
name = "v_" + FirstOrderVariable.getNumber();
}
private static int getNumber() {
return FirstOrderVariable.totalNumber++;
}
private static int totalNumber = 0;
private FirstOrderVariable(final String name) {
this.name = name;
}
}
I know I should introduce the method readObject(ObjectInputStream in),
but I am unsure what to do in there. Add the name - var mapping to the
static map, yes, but what if there already is a variable with that name?
I can’t return another object from readObject.
Of course, if I deserialize a Formula in which the variable is
contained, inside of that formula, there will only be one variable with
the given name, but if I deserialize multiple formulas, even if
originally they had the same variable, I cannot guarantee that they will
use the same variable now, can I? Even when, maybe the variable has
been created in my application already (likely for names like “x” and
“y”), these have to be the same too.
Grateful for suggestions.
H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with SUSE -
http://enigmail.mozdev.org
iEYEARECAAYFAkk1LfQACgkQBGFP0CTku6N9AgCfcQldWPfmj0 K4TU17eD0PuXjB
oR0An2DgawVz06c6xu2mPlwy+r2J/32e
=nrpG
-----END PGP SIGNATURE-----