News A server update was released on July 20. Server patch notes and downloads are here. Additionally, a client update was released on the same day. Client patch notes are here.
New scripting language support: Java
[C++] Vice Racing »

NewK

  • Jr. Member
  • Posts: 58
Re: New scripting language support: Java
« Reply #45,  »
Thank you!

One small sugestion, which would come in handy is the addition of a new utility method to the generic Entity implementation ( Entity.java and EntityImpl.java ) and that is a getDataOrDefault method. This is usually a common method in API's that deal with Object and the possible presence of null. The syntax would be something like:

getDataOrDefault(String name, Class<T> klass, Object defaultValue)

And the idea here is that if the value/data that it's retrieving is null, it would return the default value instead, the one that's being passed as a parameter.

I took a look at your EntityImpl.java and this is what I had in mind:
 
Code: [Select]
   @Override
    public <T> T getDataOrDefault(String name, Class<T> klass, Object defaultValue) {
        synchronized (sync) {
            if (data == null) {
                return null;
            }

            Object value = data.get(name);

            if (klass.isInstance(defaultValue) && value == null) {
                return (T) defaultValue;
            } else if (klass.isInstance(value)) {
                return (T) value;
            }
            return null;
        }
    }
Not something super urgent, but it'll surely help writing cleaner code in the future and avoid all those pesky null checks :-X

EK.IceFlake

  • Hero Member
  • "We are the champions my fellahs" - Sufyan/VK.SuFy
  • Posts: 1,723

[C++] Vice Racing »