[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Arraylist validation.

Posted on 2011-10-31
4
Medium Priority
?
245 Views
Last Modified: 2012-05-12
I have a arraylist of Row objects(Class definition below) and I need to do a validation on arraylist. Need to validate if all the rows have same instrument type, trade operation is sell or csell and instrument classification is PHY.

Is there a easy way to do this in java?



Class Row
{
Instrument i;
Trade t;
....
}

Open in new window

Class Instrument
{
String instrumentType;
InstrClassification classification;
.....
}

Open in new window

class Trade
{
TradeType t;
String operation;
....
}

Open in new window

class InstrClassification
{
String id
......

}

Open in new window

0
Comment
Question by:icylin
  • 2
  • 2
4 Comments
 
LVL 1

Expert Comment

by:dhruvpratap
ID: 37056559
Write an adapter class which can wrap the Row class and which implement equals() based on the fields you have mentioned(assuming they are public). Don't forget to also implement hashCode().

public boolean allRowsAreEqual(List rowList) {
        Set<WrapperRow> set = new HashSet<WrapperRow>(rowList);
        if(set.size() == 1)
            return true;
        else
           return false;
}

Open in new window


Hope this helps
0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 37060992
I think it is easier to write starightforward checks
then to invent something too fancy.
The code below is working.


import java.util.ArrayList;

public class ValidateArrayList {
     public static boolean validateList( ArrayList<Row> ar){
        ArrayList<String> inTypes = new ArrayList<String>();
        for(Row r: ar){
            if(!r.t.operation.equals("sell") && !r.t.operation.equals("csell")){
                System.out.println("Illegal trade operation");
                return false;
            }
            if(!r.i.classification.id.equals("PHY")){
                  System.out.println("Illegal classification");
                return false;
            }
            if(!inTypes.contains(r.i.instrumentType))inTypes.add(r.i.instrumentType);


        }
         if(inTypes.size()>1){
               System.out.println("more than one instrument type");
             return false;
         }
        return true;

    }




    public static void main(String[] args) {
        ArrayList<Row> ar = new ArrayList<Row>();
        InstrClassification ic1 = new  InstrClassification ("1");
            InstrClassification ic2 = new  InstrClassification ("PHY");
            InstrClassification ic3 = new  InstrClassification ("3");
          InstrClassification ic4 = new  InstrClassification ("PHY");

        TradeType  tt1 = new TradeType("a");
        TradeType tt2 = new TradeType("b");

        Trade t1 = new Trade (tt1, "sell");
         Trade t2 = new Trade (tt1, "csell");
          Trade t3= new Trade (tt2, "acsell");

        Instrument i1 = new Instrument("a", ic2);
          Instrument i2 = new Instrument("b", ic4);
         Instrument i3 = new Instrument("b", ic2);
        Row r1 = new Row(i2, t1);
        Row r2 = new Row(i2,t2);
        Row r3 = new Row(i3,t3 );

        ar.add(r1);
        ar.add(r2);
        ar.add(r3);

        if(!validateList(ar)){
            System.out.println("List is invalid");

        }   else    System.out.println("List is valid");






    }

}


 class Row {
     

  Instrument i;
Trade t;

     Row(Instrument i, Trade t){
         this.i = i;
         this.t = t;

     }


}

class Instrument {
    String instrumentType;
    InstrClassification classification;
  Instrument(String inType, InstrClassification ic){
      this.instrumentType = inType;
      this.classification = ic;
  }

}

class Trade {
 TradeType t;
String operation;
    Trade(TradeType  tt, String op){
        this.t = tt;
        this.operation = op;
    }


}

class InstrClassification
{
String id;
 InstrClassification(String id){
     this.id = id;
 }

}

class TradeType {
    String type;
    TradeType(String s){
        this.type = s;
    }

}

Open in new window



Output:
Illegal trade operation
List is invalid

Open in new window

0
 
LVL 1

Expert Comment

by:dhruvpratap
ID: 37061113
I agree.
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37061117
Thanks, dhruvpratap
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month19 days, 21 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question