commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael McGrady <m...@michaelmcgrady.com>
Subject Re: [lang] ArrayUtils.lastIndex
Date Sun, 10 Oct 2004 00:04:39 GMT
Egg Zackly!  And, if the values are stored in a common constant class 
(not interface), then some sense can be given to the whole package, 
application, or whatever the scope of the interest is, allowing people 
who use that package to integrate with the package on an intelligent 
basis. 

E.g.

package com.mysite.myconstant;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class MyConstant {

    private MyConstant() {
    }

    public static final int NOT_FOUND = -1;

    public static Map getConstantMap() {
      Map propMap = null;
      try {
        Field[] allFields = ButtonConstant.class.getDeclaredFields();
        int numFields = allFields.length;
        propMap = new HashMap(numFields);
        for(int i = 0; i < numFields; i++) {
          Field f = allFields[i];
          int mods = f.getModifiers();
          if(Modifier.isPublic(mods) && Modifier.isStatic(mods) && 
Modifier.isFinal(mods)) {
            String name = f.getName();
            Object value = f.get(null);
            propMap.put(name, value);
          }
        }
      } catch(IllegalAccessException iae) {
          // logging
      }

      return Collections.unmodifiableMap(propMap);
    }
}

Gary Gregory wrote:

>>I don't have a vote, but, if I did, 
>>    
>>
>
>Well constructed arguments are always welcome, no matter which side of
>the debate you fall on ;-)
>
>Just to give a concrete ArrayUtils example of what this would look like
>for an existing method:
>
>    public static int indexOf(float[] array, float valueToFind, int
>startIndex) {
>        if (ArrayUtils.isEmpty(array)) {
>            return NOT_FOUND;
>        }
>        if (startIndex < 0) {
>            startIndex = 0;
>        }
>        for (int i = startIndex; i < array.length; i++) {
>            if (valueToFind == array[i]) {
>                return i;
>            }
>        }
>        return NOT_FOUND;
>    }
>
>which makes it pretty clear IMO that you are returning a *well-known*
>value (whatever NOT_FOUND is) for a well-defined purpose; as opposed to
>seing "-1" and wondering, why not -2 or -3.
>
>Gary
>
>  
>
>>-----Original Message-----
>>From: Michael McGrady [mailto:mike@michaelmcgrady.com]
>>Sent: Saturday, October 09, 2004 14:53
>>To: Jakarta Commons Developers List
>>Subject: Re: [lang] ArrayUtils.lastIndex
>>
>>I don't have a vote, but, if I did, I would vote very high in favor of
>>all such decisions.  Hard coding is not good for lots of reasons, in
>>    
>>
>my
>  
>
>>opinion.  If the -1s mean the same thing "all over the place", then
>>    
>>
>they
>  
>
>>should be identified as to what they mean in all places with the value
>>put in one place.  In my opinion, this is one of the more valuable
>>    
>>
>knee
>  
>
>>jerks we should have.
>>
>>Michael McGrady
>>
>>Gary Gregory wrote:
>>
>>    
>>
>>>Speaking of "-1"s. I wonder if we should create a constant for the
>>>      
>>>
>"-1
>  
>
>>>means NOT_FOUND" concept. This value is hard coded all over the place
>>>      
>>>
>in
>  
>
>>>ArrayUtils. It seems to me like it would make the code more readable.
>>>
>>>Gary
>>>
>>>
>>>
>>>      
>>>
>>>>-----Original Message-----
>>>>From: Stephen Colebourne [mailto:scolebourne@btopenworld.com]
>>>>Sent: Saturday, October 09, 2004 04:27
>>>>To: Jakarta Commons Developers List
>>>>Subject: [lang] ArrayUtils.lastIndex
>>>>
>>>>ArrayUtils.lastIndex
>>>>This method is effectively:
>>>> array.length - 1
>>>>with null checks
>>>>
>>>>This method was added in the 1.41 version of ArrayUtils. I want to
>>>>
>>>>
>>>>        
>>>>
>>>remove
>>>
>>>
>>>      
>>>
>>>>it, as it really doesn't seem to add any value over and above
>>>>
>>>>
>>>>        
>>>>
>>>getLength().
>>>
>>>
>>>      
>>>
>>>>The original bug request didn't ask for it either:
>>>>http://issues.apache.org/bugzilla/show_bug.cgi?id=26594
>>>>
>>>>I intend to remove the method unless I hear objections.
>>>>
>>>>Stephen
>>>>
>>>>
>>>>        
>>>>
>>>---------------------------------------------------------------------
>>>      
>>>
>>>>To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
>>>>For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>>>>
>>>>
>>>>        
>>>>
>>>---------------------------------------------------------------------
>>>To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
>>>For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>>>
>>>
>>>
>>>
>>>
>>>
>>>      
>>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>>    
>>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: commons-dev-help@jakarta.apache.org
>
>
>
>
>  
>


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message