1 | /**
|
---|
2 | * Bundle class
|
---|
3 | */
|
---|
4 | package negotiator.onetomany.domain;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import negotiator.onetomany.Product;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Offers are exchanged in Bundle formats.
|
---|
15 | * A Bundle is a set of several stacks, {s_1, ..., s_n}, where no two stacks are of the same color.
|
---|
16 | * A stack can be aggregated into a bundle if at the same color of one of the Bundle's stacks.
|
---|
17 | * Two bundles can also be aggregated into a new bundle, where every two stacks of the same colors from both bundles are aggregated into one stack in the new bundle.
|
---|
18 | * That is, any t_j from the second stack is aggregated with s_i at the same color from stack 1, i.e., {s_1, ...+s_(i-1), s_i + t_j, s_(i+1), ..., s_n}.
|
---|
19 | * Aggregation of a bundle with the empty stack or empty bundle is itself.
|
---|
20 | *
|
---|
21 | * @author Faria Nassiri-Mofakham
|
---|
22 | *
|
---|
23 | */
|
---|
24 | public class Bundle {
|
---|
25 | private List<Stack> bundle;
|
---|
26 |
|
---|
27 | public Bundle()
|
---|
28 | {
|
---|
29 | this.bundle = new ArrayList<Stack>();
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * @return the bundle
|
---|
34 | */
|
---|
35 | public List<Stack> getBundle() {
|
---|
36 | return bundle;
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * @param b the bundle to set
|
---|
41 | */
|
---|
42 | public void setBundle(List<Stack> b) {
|
---|
43 | this.bundle = b;
|
---|
44 | }
|
---|
45 |
|
---|
46 | public void addStack(Stack s)
|
---|
47 | {
|
---|
48 | bundle.add(s);
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Override
|
---|
52 | public String toString()
|
---|
53 | {
|
---|
54 | return bundle.toString();
|
---|
55 | }
|
---|
56 |
|
---|
57 | public int size()
|
---|
58 | {
|
---|
59 | if (bundle != null)
|
---|
60 | {
|
---|
61 | return getBundle().size();
|
---|
62 | }
|
---|
63 | else return 0;
|
---|
64 | }
|
---|
65 |
|
---|
66 | public Stack getStack(int i)
|
---|
67 | {
|
---|
68 | return getBundle().get(i);
|
---|
69 | }
|
---|
70 |
|
---|
71 | public Stack iterator()
|
---|
72 | {
|
---|
73 | Stack s=null;
|
---|
74 | if (this!=null)
|
---|
75 | for (int i=0; i<this.size();i++)
|
---|
76 | s = bundle.get(i);
|
---|
77 | return s;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | /**
|
---|
82 | *
|
---|
83 | * bundle "+" bundle
|
---|
84 | *
|
---|
85 | * @param b, this method adds bundle b to `this' bundle
|
---|
86 | * @return the aggregated bundle
|
---|
87 | * where, any two stacks of the same colors are aggregated as a new stack into the new bundle, otherwise, they are just added into the new bundle.
|
---|
88 | *
|
---|
89 | * This method just calls stack '+' stack aggregation operation (has no need to bundle "+" stack aggregation operation.
|
---|
90 | */
|
---|
91 | public Bundle aggregateWith(Bundle b) //This WORKED WELL although it is not in nice programming style!
|
---|
92 | {
|
---|
93 | if (b!=null && this!=null)
|
---|
94 | {
|
---|
95 | Bundle l=new Bundle();
|
---|
96 | Boolean[] f1 = new Boolean[b.size()]; //flag for b bundle stack added
|
---|
97 | Arrays.fill(f1, Boolean.FALSE);
|
---|
98 | Boolean[] f2 = new Boolean[this.size()]; //flag for `this' bundle stack added
|
---|
99 | Arrays.fill(f2, Boolean.FALSE);
|
---|
100 |
|
---|
101 |
|
---|
102 | for (int i=0;i<b.size();i++) //loop over bundle b stacks
|
---|
103 | {
|
---|
104 | Stack s=b.getStack(i);
|
---|
105 |
|
---|
106 | for (int j=0; j<this.size();j++) //loop over `this' bundle stacks
|
---|
107 | {
|
---|
108 | Stack t= this.getStack(j);
|
---|
109 | if (!f1[i] && !f2[j] && t.getChip().getColor()==s.getChip().getColor())
|
---|
110 | {
|
---|
111 | l.addStack(t.aggregateWith(s)); //aggregate these two stacks (b's i-th and this j-th) into the new bundle l
|
---|
112 | f1[i]=Boolean.TRUE;
|
---|
113 | f2[j]=Boolean.TRUE;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | if (!f1[i])
|
---|
117 | l.addStack(s); // so put i-th stack of bundle b directly into the new bundle
|
---|
118 | }
|
---|
119 | for (int j=0;j<this.size();j++)
|
---|
120 | if (!f2[j])
|
---|
121 | l.addStack(this.getStack(j)); // so put j-th stack of `this' bundle directly into the new bundle
|
---|
122 | return l;
|
---|
123 | }
|
---|
124 | return this;
|
---|
125 | }
|
---|
126 |
|
---|
127 | // /**
|
---|
128 | // * bundle "+" bundle : second method (nested iterators by the other method), doesn't work correctly.
|
---|
129 | // *
|
---|
130 | // * @param b, this method adds bundle b to `this' bundle
|
---|
131 | // * @return the aggregated bundle
|
---|
132 | // * where, any two stacks of the same colors are aggregated as a new stack into the new bundle, otherwise, they are just added into the new bundle.
|
---|
133 | // *
|
---|
134 | // * This method calls bundle "+" stack aggregation operation.
|
---|
135 | // */
|
---|
136 | // public Bundle aggregateWith333(Bundle b)
|
---|
137 | // {
|
---|
138 | // if (b!=null && this!=null)
|
---|
139 | // {
|
---|
140 | // Bundle l=new Bundle();
|
---|
141 | //
|
---|
142 | // for (Stack s : b.getBundle())
|
---|
143 | // {
|
---|
144 | // l=this.aggregateWith(s);
|
---|
145 | // }
|
---|
146 | // return l;
|
---|
147 | // }
|
---|
148 | // return this;
|
---|
149 | // }
|
---|
150 | //
|
---|
151 | //
|
---|
152 | //
|
---|
153 | // /**
|
---|
154 | // * bundle '+' stack
|
---|
155 | // *
|
---|
156 | // * @param s, this method adds stack s to `this' bundle
|
---|
157 | // * @return the aggregated bundle
|
---|
158 | // * where, stack s is aggregated with a stack of the same color in `this' bundle and the new stack is added into the new bundle;
|
---|
159 | // * otherwise, stack s is just added into the new bundle.
|
---|
160 | // *
|
---|
161 | // * This method is called by
|
---|
162 | // */
|
---|
163 | // public Bundle aggregateWith(Stack s)
|
---|
164 | // {
|
---|
165 | // if (s!=null && this!=null)
|
---|
166 | // {
|
---|
167 | // Bundle b=new Bundle();
|
---|
168 | // Boolean f=false; //flag for s stack added
|
---|
169 | //
|
---|
170 | // for (Stack t : this.getBundle()) //loop over `this' bundle stacks
|
---|
171 | // {
|
---|
172 | // if (t.getChip().getColor()==s.getChip().getColor())
|
---|
173 | // {
|
---|
174 | // b.addStack(t.aggregateWith(s));
|
---|
175 | // f=true;
|
---|
176 | // }
|
---|
177 | // else
|
---|
178 | // b.addStack(t); // so put the stack of `this' bundle directly into the new bundle
|
---|
179 | // }
|
---|
180 | // if (!f) //s not aggregated with any stack in `this' bundle
|
---|
181 | // b.addStack(s); // so put stack s directly into the new bundle
|
---|
182 | // return b;
|
---|
183 | // }
|
---|
184 | // return this;
|
---|
185 | // }
|
---|
186 | }
|
---|