001 package market.swing; 002 003 import java.io.Serializable; 004 import java.util.Comparator; 005 006 import market.CIArticle; 007 import market.Conversions; 008 import data.swing.CountingStockTableModel; 009 010 /** 011 * Compares {@link market.CIArticle CIArticles} by numbers. 012 */ 013 public class CmpNumbers implements Comparator, Serializable { 014 015 public static final int BID = 0; 016 public static final int OFFER = 1; 017 public static final int COUNT = 2; 018 public static final boolean SUMMED = true; 019 public static final boolean SINGLE = false; 020 021 private int chosen; 022 private boolean summed; 023 024 /** 025 * @param chosen <ul><li>0: CIArticles are compared by their bid</li> 026 * <li>1: CIArticles are compared by their offer</li> 027 * <li>2: CIArticles are compared by their amount</li></ul> 028 */ 029 030 public CmpNumbers(int chosen) { 031 this.chosen = chosen; 032 this.summed = false; 033 } 034 035 /** 036 * @param chosen <ul><li>0: CIArticles are compared by their bid</li> 037 * <li>1: CIArticles are compared by their offer</li> 038 * <li>2: CIArticles are compared by their amount</li></ul> 039 * 040 * @param summed Tells the comparator if value to be sorted has been multiplied with the amount 041 * of items of a CountingStock or not. This depends, wether the corresponding table shows just the 042 * single price of an Article or the summed up prize of a whole bunch of Articles of a kind. 043 * This parameter only takes effect for CountingStockRecords 044 */ 045 public CmpNumbers(int chosen, boolean summed) { 046 this.chosen = chosen; 047 this.summed = summed; 048 } 049 050 /** 051 * The actual comparison. 052 * @param o1 the first CIArticle. 053 * @param o2 the second CIArticle. 054 * @return an int representing the result of the comparison. 055 */ 056 public int compare(Object o1, Object o2) { 057 CIArticle c1 = Conversions.recordToCIArticle(o1); 058 CIArticle c2 = Conversions.recordToCIArticle(o2); 059 String name1 = c1.getName(); 060 String name2 = c2.getName(); 061 int count1 = 1; 062 int count2 = 1; 063 int amount1 = 1; 064 int amount2 = 1; 065 //CountingStockTableObjects have extra value count, so evaluate them first 066 if (o1 instanceof CountingStockTableModel.Record) { 067 count1 = ((CountingStockTableModel.Record)o1).getCount(); 068 count2 = ((CountingStockTableModel.Record)o2).getCount(); 069 } 070 if (summed) { 071 amount1 = count1; 072 amount2 = count2; 073 } 074 switch (chosen) { 075 case BID: return amount1 * c1.getBid() - amount2 * c2.getBid(); 076 case OFFER: return amount1 * c1.getOffer() - amount2 * c2.getOffer(); 077 case COUNT: return count1 - count2; 078 } 079 return 0; 080 } 081 }