[josm-dev] Jumbo Patch
Petr Nejedly
Petr.Nejedly at Sun.COM
Sun Dec 16 22:09:30 GMT 2007
Dave Hansen napsal(a):
> On Sun, 2007-12-16 at 01:11 +0100, Petr Nejedly wrote:
>> Dave Hansen napsal(a):
>>> Privatize Way.nodes. We need to do this to keep the cache consistent.
>>> We also need a ton of new accessor functions. These need some more
>>> work. This is the bulk of the "core" patch.
>> While I have already voiced in my opinion on encapsulation and how
>> badly is it needed in JOSM, I must say that your approach is certainly
>> far from what would I do.
>> Why don't you simply export read-only view over the nodes list
>> (Collections.unmodifiableList(nodes))? That would cover most of the
>> use cases and you can get rid or many of the methods. And there are
>> ways how to do even the modification accesses w/o so many methods.
>
> I suck at Java. I'm an idiot. Could you elaborate on this a bit more?
You basically need (minimalist version, but good enough [*]):
class Way {
private List<Node> secret_nodes;
/** @returns a read-only view of the nodes
*/
public List<Node> getNodes() {
// no need to keep the instance, it is cheap to create
// and it is expected to live shortly
return Collections.unmodifiableList(secret_nodes);
}
public void setNodes(Collection<Node> newNodes) {
// perform the necessary update of other strucures
secret_nodes.clear();
secret_nodes.addAll(newNodes);
}
...
}
[*]: It is less effective than, say, your nodes.add(n, idx), but
a collection of nodes is usually small and a modification
rate is very slow (typically a user manually editing the map).
For more (programmatic) edits from single place, you'd push them
all at the end:
List<Node> copy = new {Linked|Array}List(w.getNodes());
// do watever with the copy
w.setNodes(copy);
>> And I have to oppose strongly (as a user) against Node.wayList.
> I guess we could just generate it when we need it, such as during
> validation. If you use the validator plugin, you're going to get at
> least a couple of checks doing exactly this operation, but duplicated
> several times.
Computing the cache at the beginning of the validation shouldn't be
that slow. At worst, there can be a private field (type: Object) that would
either contain directly the Way (if there is only one, as for most Nodes)
or a collection of Ways (if there are more of them).
--
Petr "Nenik" Nejedly, NetBeans/Sun Microsystems, http://www.netbeans.org
355/113 -- Not the famous irrational number PI, but an incredible simulation!
More information about the josm-dev
mailing list