Overloading for great power!



  • Integer _specialTag = null;
    
    public void setSpecialTag(String value) {
      try {
        _setSpecialTag(Integer.valueOf(value));
      } catch (NumberFormatException e) {
        _setSpecialTag((Integer) null);
      }
    }
    
    public void setSpecialTag(Integer value) {
      if (value == null) {
        _setSpecialTag(value);
      } else {
        _setSpecialTag(Integer.valueOf(value.intValue());
      }
    }
    
    public void setSpecialTag(int value) {
      _setSpecialTag(Integer.valueOf(value));
    }
    
    private void _setSpecialTag(Integer value) {
      _specialTag = value;
    }
    

    Granted this predates auto-boxing/unboxing ... but still!



  • @zelmak said:

    _setSpecialTag(Integer.valueOf(value.intValue());

    So, wait, first we unbox the integer... then we box it back up again in a brand new box? Is this supposed to be some sort of magic ward against the "spooky action at a distance" that occurs when you pass object references around? I guess that kind of makes sense, but it still seems a bit smelly...



  • Wait a minute, so if I call it with setSpecialValue(10) it will construct an Integer object, just to take valueOf it.

    Also like how it does have a special case for null.

     



  • @mt@ilovefactory.com said:

    Wait a minute, so if I call it with setSpecialValue(10) it will construct an Integer object, just to take valueOf it.

    No, it won't. The int overload just constructs the Integer object and assigns it to the field, without any valueOf silliness.



  • @ekolis said:

    @mt@ilovefactory.com said:
    Wait a minute, so if I call it with setSpecialValue(10) it will construct an Integer object, just to take valueOf it.

    No, it won't. The int overload just constructs the Integer object and assigns it to the field, without any valueOf silliness.

    Oh you are right. There is both a setSpecialTagand then the private _setSpecialTag method. Don't know how I could confuse the to.



  • @ekolis said:

    @zelmak said:
    _setSpecialTag(Integer.valueOf(value.intValue());

    So, wait, first we unbox the integer... then we box it back up again in a brand new box? Is this supposed to be some sort of magic ward against the "spooky action at a distance" that occurs when you pass object references around? I guess that kind of makes sense, but it still seems a bit smelly...

    I know what you mean, but someone should have told the developer that Integer is immutable...


Log in to reply