source: src/main/java/negotiator/onetomany/domain/Bundle.java@ 242

Last change on this file since 242 was 240, checked in by Tim Baarslag, 5 years ago

Bundle is now iterable, by iterating over all the Stacks within it

File size: 6.5 KB
Line 
1/**
2 * Bundle class
3 */
4package negotiator.onetomany.domain;
5
6import java.util.List;
7
8import java.util.ArrayList;
9import java.util.Arrays;
10import java.util.Iterator;
11
12/**
13 * Offers are exchanged in Bundle formats.
14 * A Bundle is a set of several stacks, {s_1, ..., s_n}, where no two stacks are of the same color.
15 * A stack can be aggregated into a bundle if at the same color of one of the Bundle's stacks.
16 * 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.
17 * 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}.
18 * Aggregation of a bundle with the empty stack or empty bundle is itself.
19 *
20 * @author Faria Nassiri-Mofakham
21 *
22 */
23public class Bundle implements Iterable<Stack>
24{
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 {
37 return bundle;
38 }
39
40 /**
41 * @param b the bundle to set
42 */
43 public void setBundle(List<Stack> b)
44 {
45 this.bundle = b;
46 }
47
48 public void addStack(Stack s)
49 {
50 bundle.add(s);
51 }
52
53 @Override
54 public String toString()
55 {
56 return bundle.toString();
57 }
58
59 public int size()
60 {
61 if (bundle != null)
62 {
63 return getBundle().size();
64 }
65 else return 0;
66 }
67
68 public Stack getStack(int i)
69 {
70 return getBundle().get(i);
71 }
72
73 public Iterator<Stack> iterator()
74 {
75 return bundle.iterator();
76 }
77
78
79 /**
80 *
81 * bundle "+" bundle
82 *
83 * @param b, this method adds bundle b to `this' bundle
84 * @return the aggregated bundle
85 * 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.
86 *
87 * This method just calls stack '+' stack aggregation operation (has no need to bundle "+" stack aggregation operation.
88 */
89 public Bundle aggregateWith(Bundle b) //This WORKED WELL although it is not in nice programming style!
90 {
91 if (b!=null && this!=null)
92 {
93 Bundle l=new Bundle();
94 Boolean[] f1 = new Boolean[b.size()]; //flag for b bundle stack added
95 Arrays.fill(f1, Boolean.FALSE);
96 Boolean[] f2 = new Boolean[this.size()]; //flag for `this' bundle stack added
97 Arrays.fill(f2, Boolean.FALSE);
98
99
100 for (int i=0;i<b.size();i++) //loop over bundle b stacks
101 {
102 Stack s=b.getStack(i);
103
104 for (int j=0; j<this.size();j++) //loop over `this' bundle stacks
105 {
106 Stack t= this.getStack(j);
107 if (!f1[i] && !f2[j] && t.getChip().getColor()==s.getChip().getColor())
108 {
109 l.addStack(t.aggregateWith(s)); //aggregate these two stacks (b's i-th and this j-th) into the new bundle l
110 f1[i]=Boolean.TRUE;
111 f2[j]=Boolean.TRUE;
112 }
113 }
114 if (!f1[i])
115 l.addStack(s); // so put i-th stack of bundle b directly into the new bundle
116 }
117 for (int j=0;j<this.size();j++)
118 if (!f2[j])
119 l.addStack(this.getStack(j)); // so put j-th stack of `this' bundle directly into the new bundle
120 return l;
121 }
122 return this;
123 }
124
125// new try for calling two methods both without using iterators
126//
127// public Bundle aggregateWith2(Bundle b) //This WORKED WELL although it is not in nice programming style!
128// {
129// if (b!=null && this!=null)
130// {
131// Bundle l=new Bundle();
132// Boolean[] f1 = new Boolean[b.size()]; //flag for b bundle stack added
133// Arrays.fill(f1, Boolean.FALSE);
134// Boolean[] f2 = new Boolean[this.size()]; //flag for `this' bundle stack added
135// Arrays.fill(f2, Boolean.FALSE);
136//
137//
138// for (int i=0;i<b.size();i++) //loop over bundle b stacks
139// {
140// Stack s=b.getStack(i);
141//
142// l=this.aggregateWith2(s,f2);
143// }
144// for (int j=0;j<this.size();j++)
145// if (!f2[j])
146// l.addStack(this.getStack(j)); // so put j-th stack of `this' bundle directly into the new bundle
147// return l;
148// }
149// return this;
150// }
151//
152// public Bundle aggregateWith2(Stack s, Boolean[] f)
153// {
154// if (s!=null && this!=null)
155// {
156// Bundle b=new Bundle();
157// Boolean g=Boolean.FALSE; //flag for s stack added
158// Arrays.fill(f, Boolean.FALSE);//flag for `this' bundle stack added
159//
160// for (int i=0; i<this.size();i++) //loop over `this' bundle stacks
161// {
162// Stack t= this.getStack(i);
163// if (!g && !f[i] && t.getChip().getColor()==s.getChip().getColor())
164// {
165// b.addStack(t.aggregateWith(s)); //aggregate these two stacks (b's i-th and this j-th) into the new bundle l
166// g=Boolean.TRUE;
167// f[i]=Boolean.TRUE;
168// }
169// }
170// if (!g)
171// b.addStack(s); // so put i-th stack of bundle b directly into the new bundle
172// }
173// return b,f;
174// }
175// return this,f;
176// }
177
178
179// previous try for calling two methods both with using iterators
180//
181 /**
182 * bundle "+" bundle : second method (nested iterators by the other method), doesn't work correctly.
183 *
184 * @param b, this method adds bundle b to `this' bundle
185 * @return the aggregated bundle
186 * 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.
187 *
188 * This method calls bundle "+" stack aggregation operation.
189 */
190// public Bundle aggregateWith333(Bundle b)
191// {
192// Bundle l=new Bundle();
193// for (Stack s : b)
194// l.aggregateWith(s);
195// return l;
196// }
197//
198//
199//
200// /**
201// * bundle '+' stack
202// *
203// * @param s, this method adds stack s to `this' bundle
204// * @return the aggregated bundle
205// * 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;
206// * otherwise, stack s is just added into the new bundle.
207// *
208// * This method is called by
209// */
210// public Bundle aggregateWith(Stack s)
211// {
212// if (s!=null && this!=null)
213// {
214// Bundle b=new Bundle();
215// Boolean f=false; //flag for s stack added
216//
217// for (Stack t : this.getBundle()) //loop over `this' bundle stacks
218// {
219// if (t.getChip().getColor()==s.getChip().getColor())
220// {
221// b.addStack(t.aggregateWith(s));
222// f=true;
223// }
224// else
225// b.addStack(t); // so put the stack of `this' bundle directly into the new bundle
226// }
227// if (!f) //s not aggregated with any stack in `this' bundle
228// b.addStack(s); // so put stack s directly into the new bundle
229// return b;
230// }
231// return this;
232// }
233}
Note: See TracBrowser for help on using the repository browser.