Querz / NBT

A java implementation of the NBT protocol, including a way to implement custom tags.
MIT License
183 stars 48 forks source link

Only implement Comparable where needed #23

Closed Marcono1234 closed 5 years ago

Marcono1234 commented 5 years ago

It might be good to implement Comparable only where needed and not for all Tags. Currently the behavior is also a little bit inconsistent since some implementations can throw exceptions while others, such as CompoundTag just return 0.

Querz commented 5 years ago

compareTo should throw an exception when comparing incompatible types. See #27.

Marcono1234 commented 5 years ago

But why is Comparable needed on Tag level anyways? Would there be any problems when moving it the respective classes (except compatibility)?

Querz commented 5 years ago

I just saw that i forgot to add a ListTag#sort() for which this was originally intended. In this case it makes sense to have every tag (or their abstract parent class, like ArrayTag or NumberTag) implement Comparable.

Marcono1234 commented 5 years ago

This should also be possible using sort(Comparator.naturalOrder()), but would of course be more to type.

Querz commented 5 years ago

For sort(Comparator.naturalOrder() to work i would need to implement Comparable for every tag anyway. Or is your point to have some tags not comparable at all, because it might not make much sense (e.g. comparing CompoundTag)?

Marcono1234 commented 5 years ago

Using sort(Comparator.naturalOrder()) would only require that the currently used element type implements Comparable.

My goals are to

Querz commented 5 years ago

See #27 Comparable is only implemented by Tags that need it. Implementing Comparable for EndTag didn't really make sense :)

Marcono1234 commented 5 years ago

I am still not sure if the implementations for the array, list and compound tags are not too specific and tailored to a specific use case only. But this is definitely better, thanks!