1 | /**
|
---|
2 | * Bundle class
|
---|
3 | */
|
---|
4 | package negotiator.onetomany.domain;
|
---|
5 |
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.List;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * Offers are exchanged in Bundle formats.
|
---|
11 | * A Bundle is a set of several stacks, {s_1, ..., s_n}, where no two stacks are of the same color.
|
---|
12 | * A stack can be aggregated into a bundle if at the same color of one of the Bundle's stacks.
|
---|
13 | * 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.
|
---|
14 | * 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}.
|
---|
15 | * Aggregation of a bundle with the empty stack or empty bundle is itself.
|
---|
16 | *
|
---|
17 | * @author Faria Nassiri-Mofakham
|
---|
18 | *
|
---|
19 | */
|
---|
20 | public class Bundle {
|
---|
21 | private List<Stack> bundle;
|
---|
22 |
|
---|
23 | public Bundle()
|
---|
24 | {
|
---|
25 | this.bundle = new ArrayList<Stack>();
|
---|
26 | }
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * @return the bundle
|
---|
30 | */
|
---|
31 | public List<Stack> getBundle() {
|
---|
32 | return bundle;
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * @param b the bundle to set
|
---|
37 | */
|
---|
38 | public void setBundle(List<Stack> b) {
|
---|
39 | this.bundle = b;
|
---|
40 | }
|
---|
41 |
|
---|
42 | public void addStack(Stack s)
|
---|
43 | {
|
---|
44 | bundle.add(s);
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public String toString()
|
---|
49 | {
|
---|
50 | return bundle.toString();
|
---|
51 | }
|
---|
52 |
|
---|
53 | public int size()
|
---|
54 | {
|
---|
55 | if (bundle != null)
|
---|
56 | {
|
---|
57 | return getBundle().size();
|
---|
58 | }
|
---|
59 | else return 0;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public Stack getStack(int i)
|
---|
63 | {
|
---|
64 | return getBundle().get(i);
|
---|
65 | }
|
---|
66 |
|
---|
67 | public Stack iterator() // stack or List<stack>
|
---|
68 | {
|
---|
69 | Stack s=null;
|
---|
70 | if (this!=null)
|
---|
71 | for (int i=0; i<this.size();i++)
|
---|
72 | s = bundle.get(i);
|
---|
73 | return s; // this just returns the last one, a stack, not a list!
|
---|
74 | }
|
---|
75 |
|
---|
76 | // /**
|
---|
77 | // * @param s, this method adds stack s to `this' bundle
|
---|
78 | // * @return the aggregated bundle
|
---|
79 | // * 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;
|
---|
80 | // * otherwise, stack s is just added into the new bundle.
|
---|
81 | // */
|
---|
82 | // public Bundle aggregateWith(Stack s)
|
---|
83 | // {
|
---|
84 | // if (s!=null)
|
---|
85 | // {
|
---|
86 | // Bundle b=new Bundle();
|
---|
87 | // Boolean f=false; //stack s not aggregated yet
|
---|
88 | // if (this!=null)
|
---|
89 | // {
|
---|
90 | // Stack t=new Stack(); //stacks of `this' bundle
|
---|
91 | // for (int i=0; i<this.size();i++)
|
---|
92 | // {
|
---|
93 | // t=this.getStack(i);
|
---|
94 | // if (f || (t.getChip().getColor()!=s.getChip().getColor()) )
|
---|
95 | // {
|
---|
96 | // b.addStack(t);
|
---|
97 | // System.out.println("\nPOINT1-0:BundleClass:aggregateWith(stack), produced bundle "+b);
|
---|
98 | // }
|
---|
99 | // else
|
---|
100 | // {
|
---|
101 | // b.addStack(t.aggregateWith(s));
|
---|
102 | // f=true; // stack s was aggregated with stack t of `this' bundle into new bundle b
|
---|
103 | // System.out.println("\nPOINT1-00:BundleClass:aggregateWith(stack), produced bundle "+b);
|
---|
104 | // }
|
---|
105 | // }
|
---|
106 | // }
|
---|
107 | // if (!f || (this==null))
|
---|
108 | // b.addStack(s);
|
---|
109 | // System.out.println("\nPOINT1:BundleClass:aggregateWith(stack), produced bundle "+b);
|
---|
110 | // return b;
|
---|
111 | // }
|
---|
112 | // return this;
|
---|
113 | // }
|
---|
114 | //
|
---|
115 | // /**
|
---|
116 | // * @param b, this method adds bundle b to `this' bundle
|
---|
117 | // * @return the aggregated bundle
|
---|
118 | // * 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.
|
---|
119 | // */
|
---|
120 | // public Bundle aggregateWith(Bundle b)
|
---|
121 | // {
|
---|
122 | // System.out.println("\nPOINT2:BundleClass:aggregateWith(Bundle)method, `this' is: "+this+"\nand b is: "+b);
|
---|
123 | // if (b!=null)
|
---|
124 | // {
|
---|
125 | // Bundle l=new Bundle();
|
---|
126 | // Stack s=new Stack(); //stacks of bundle b
|
---|
127 | // for (int i=0; i<b.size();i++) // i over stacks of bundle b
|
---|
128 | // {
|
---|
129 | // s=b.getStack(i);
|
---|
130 | // if (this!=null)
|
---|
131 | // {
|
---|
132 | // l.aggregateWith(s);
|
---|
133 | // System.out.println("\nPOINT3-0:BundleClass:aggregateWith(Bundle)method, produced bundle is "+l);
|
---|
134 | // }
|
---|
135 | // else
|
---|
136 | // l.addStack(s);
|
---|
137 | // System.out.println("\nPOINT3:BundleClass:aggregateWith(Bundle)method, produced bundle is "+l);
|
---|
138 | // }
|
---|
139 | // return l;
|
---|
140 | // }
|
---|
141 | // return this;
|
---|
142 | // }
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * @param s, this method adds stack s to `this' bundle
|
---|
146 | * @return the aggregated bundle
|
---|
147 | * 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;
|
---|
148 | * otherwise, stack s is just added into the new bundle.
|
---|
149 | */
|
---|
150 | public Bundle aggregateWith(Stack s)
|
---|
151 | {
|
---|
152 | if (s!=null)
|
---|
153 | {
|
---|
154 | Bundle b=new Bundle();
|
---|
155 | Boolean f=false; //stack s not aggregated yet
|
---|
156 | if (this!=null)
|
---|
157 | {
|
---|
158 | Stack t=new Stack(); //stacks of `this' bundle
|
---|
159 | for (int i=0; i<this.size();i++)
|
---|
160 | {
|
---|
161 | t=this.getStack(i);
|
---|
162 | if (f || (t.getChip().getColor()!=s.getChip().getColor()) ) // if stack s already aggregated into new bundle (i.e., f=true), next stacks of `this' bundle still would be added to the new bundle
|
---|
163 | {
|
---|
164 | b.addStack(t);
|
---|
165 | System.out.println("\nPOINT1-0:BundleClass:aggregateWith(stack), produced bundle "+b);
|
---|
166 | }
|
---|
167 | else
|
---|
168 | if (t.getChip().getColor()!=s.getChip().getColor())
|
---|
169 | {
|
---|
170 | b.addStack(t.aggregateWith(s));
|
---|
171 | f=true; // stack s was aggregated with stack t of `this' bundle into new bundle b
|
---|
172 | System.out.println("\nPOINT1-00:BundleClass:aggregateWith(stack), produced bundle "+b);
|
---|
173 | }
|
---|
174 | }
|
---|
175 | if (!f)
|
---|
176 | {
|
---|
177 | b.addStack(s);
|
---|
178 | f=true; // stack s was aggregated with stack t of `this' bundle into new bundle b
|
---|
179 | System.out.println("\nPOINT1-000:BundleClass:aggregateWith(stack), produced bundle "+b);
|
---|
180 | }
|
---|
181 | return b;
|
---|
182 | }
|
---|
183 | this.addStack(s); //`this' bundle will no longer be null; stack s is added
|
---|
184 | f=true; // stack s was aggregated into `this' bundle
|
---|
185 | System.out.println("\nPOINT1:BundleClass:aggregateWith(stack), produced bundle "+b);
|
---|
186 | }
|
---|
187 | return this;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * @param b, this method adds bundle b to `this' bundle
|
---|
192 | * @return the aggregated bundle
|
---|
193 | * 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.
|
---|
194 | */
|
---|
195 | public Bundle aggregateWith(Bundle b)
|
---|
196 | {
|
---|
197 | System.out.println("\nPOINT2:BundleClass:aggregateWith(Bundle)method, `this' is: "+this+"\nand b is: "+b);
|
---|
198 | if (b!=null)
|
---|
199 | {
|
---|
200 | Bundle l=new Bundle();
|
---|
201 | Stack s=new Stack(); //stacks of bundle b
|
---|
202 | for (int i=0; i<b.size();i++) // i over stacks of bundle b
|
---|
203 | {
|
---|
204 | s=b.getStack(i);
|
---|
205 | if (this!=null)
|
---|
206 | {
|
---|
207 | l.aggregateWith(s);
|
---|
208 | System.out.println("\nPOINT3-0:BundleClass:aggregateWith(Bundle)method, produced bundle is "+l);
|
---|
209 | }
|
---|
210 | else
|
---|
211 | l.addStack(s);
|
---|
212 | System.out.println("\nPOINT3:BundleClass:aggregateWith(Bundle)method, produced bundle is "+l);
|
---|
213 | }
|
---|
214 | return l;
|
---|
215 | }
|
---|
216 | return this;
|
---|
217 | }
|
---|
218 | }
|
---|