views:

112

answers:

5

I have the folowing interface;

public static interface Attributes
{
        public final static String InterestDeterminationDate = "InterestDeterminationDate";
        public final static String CreditType = "CreditType";
        public final static String NumberInternal = "NumberInternal";
        public final static String InterestRate = "InterestRate";
        public final static String RemainingDebtAmount = "RemainingDebtAmount";
        public final static String ConsumerPart = "ConsumerPart";
        public final static String TechnicalProductName = "TechnicalProductName";
        public final static String TermOfDuration = "TermOfDuration";
        public final static String PeriodInterestTaxReduction = "PeriodInterestTaxReduction";
        public final static String OriginMark = "OriginMark";
        public final static String Currency = "Currency";
        public final static String PenaltyRuleId = "PenaltyRuleId";
        public final static String InstallmentCalculationMethod = "InstallmentCalculationMethod";
        public final static String InterestRenewalDate = "InterestRenewalDate";
        public final static String TechnicalProductDescription = "TechnicalProductDescription";
        public final static String TechnicalProductDate = "TechnicalProductDate";
        public final static String CollectionIntervalPeriod = "CollectionIntervalPeriod";
        public final static String Enddate = "Enddate";
}

I need to check is a given string is a part of this Attributes Interface.

How can i check this?

Regards, bas Hendriks

A: 

You can use the reflection API, and the "getFields()" method of the Class class.

Then you check the field name with the "getName()" method of the Field class.

Here is the Oracle official tutorial.

Benoit Courtine
+5  A: 

If you really want todo this, then you should use reflection and go through all the values in Attributes.

A better way to do this would be the use of enums :

public enum Attributes{
    InterestDeterminationDate,
    CreditType,
    NumberInternal,
    InterestRate,
    RemainingDebtAmount,
    ConsumerPart,
    TechnicalProductName,
    TermOfDuration,
    PeriodInterestTaxReduction,
    OriginMark,
    Currency,
    PenaltyRuleId,
    InstallmentCalculationMethod,
    InterestRenewalDate,
    TechnicalProductDescription,
    TechnicalProductDate,
    CollectionIntervalPeriod,
    Enddate;
}

and the Attributes.valueOf(yourVariable); would check this for you.

Beware with enum, the valueOf() method will throw a IllegalArgumentException if yourVariable isn't in Attributes. Plus you yourVariable isn't null or you will have to handle a NullPointerException

Colin Hebert
@BasHendriks: Also, if you want to stick with Strings (and reflection), it is better to put them in a final class rather than an interface. Interfaces are not meant for this purpose.
abhin4v
I agree that enums should be the right way to do this, but i have two limitations: I cannot change the interface. I have to use this. And i'm limited to java 1.4.......
Bas Hendriks
Then look at other solutions. But you should specify your limitation when you ask a question.
Colin Hebert
A: 

You can build a set using reflection and test against that set:

Class<Attributes> attr = Attributes.class;
Field[] fields = attr.getDeclaredFields();
final Set<String> fieldsInAttributes = new HashSet<String>();
for (Field field : fields) {
    fieldsInAttributes.add(field.getName());
}

System.out.println(fieldsInAttributes.contains("PenaltyRuleId"));
zevra0
This will search the field names for the target string. I think he wants the field values, rather than the names.
Rob Hruska
They're the same in the example but, yeah, you're probably right.
zevra0
A: 

Your question doesn't make it clear whether you're trying to find out if the query string is the property name or value. If you're trying to find out if it's a value, the following will work:

public static boolean hasValue(String value) throws IllegalAccessException {
    for(Field field : Attributes.class.getDeclaredFields()) {
        if(((String)field.get(Attributes.class)).equals(value)) {
            return true;
        }
    }
    return false;
}

However, I would advise following Colin's suggestion of using an Enum, it will be easier for you to work with in the future.

Rob Hruska
I agree with Colin. His suggestion is better, but i'm limited to java 1.4 (and cannot alter the interface)
Bas Hendriks
A: 
public static String getFieldName(String fieldValue) throws Exception {
  for (Field field : Attributes.class.getFields()) 
    if (fieldValue.equals(field.get(null)))
      return field.getName();
  return null;
}
erickson