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