001package com.avaje.ebean.bean;
002
003import java.io.Serializable;
004import java.util.Collection;
005import java.util.Set;
006
007import com.avaje.ebean.ExpressionList;
008
009/**
010 * Lazy loading capable Maps, Lists and Sets.
011 * <p>
012 * This also includes the ability to listen for additions and removals to or
013 * from the Map Set or List. The purpose of gathering the additions and removals
014 * is to support persisting ManyToMany objects. The additions and removals
015 * become inserts and deletes from the intersection table.
016 * </p>
017 * <p>
018 * Technically this is <em>NOT</em> an extension of
019 * <em>java.util.Collection</em>. The reason being that java.util.Map is not a
020 * Collection. I realise this makes this name confusing so I apologise for that.
021 * </p>
022 */
023public interface BeanCollection<E> extends Serializable {
024
025  enum ModifyListenMode {
026    /** The common mode */
027    NONE,
028    /** Mode used for PrivateOwned */
029    REMOVALS,
030    /** Mode used for ManyToMany relationships */
031    ALL
032  }
033
034  /**
035   * Load bean from another collection.
036   */
037  void loadFrom(BeanCollection<?> other);
038
039  /**
040   * Add a bean to the list/set with modifyListen notification.
041   */
042  void addBean(E bean);
043
044  /**
045   * Remove a bean to the list/set with modifyListen notification.
046   */
047  void removeBean(E bean);
048
049  /**
050   * Reset the collection back to an empty state ready for reloading.
051   * <p>
052   * This is done as part of bean refresh.
053   */
054  void reset(EntityBean ownerBean, String propertyName);
055
056  /**
057   * Return true if the collection is empty and untouched. Used to detect if a
058   * collection was 'cleared' deliberately or just un-initialised.
059   */
060  boolean isEmptyAndUntouched();
061
062  /**
063   * Return the bean that owns this collection.
064   */
065  EntityBean getOwnerBean();
066
067  /**
068   * Return the bean property name this collection represents.
069   */
070  String getPropertyName();
071
072  /**
073   * Check after the lazy load that the underlying collection is not null
074   * (handle case where join to many not outer).
075   * <p>
076   * That is, if the collection was not loaded due to filterMany predicates etc
077   * then make sure the collection is set to empty.
078   * </p>
079   */
080  boolean checkEmptyLazyLoad();
081
082  /**
083   * Return the filter (if any) that was used in building this collection.
084   * <p>
085   * This is so that the filter can be applied on refresh.
086   * </p>
087   */
088  ExpressionList<?> getFilterMany();
089
090  /**
091   * Set the filter that was used in building this collection.
092   */
093  void setFilterMany(ExpressionList<?> filterMany);
094
095  /**
096   * Set a listener to be notified when the BeanCollection is first touched.
097   */
098  void setBeanCollectionTouched(BeanCollectionTouched notify);
099
100  /**
101   * Return true if the collection has been registered with the batch loading context.
102   */
103  boolean isRegisteredWithLoadContext();
104
105  /**
106   * Set the loader that will be used to lazy/query load this collection.
107   * <p>
108   * This is effectively the batch loading context this collection is registered with.
109   * </p>
110   */
111  void setLoader(BeanCollectionLoader beanLoader);
112
113  /**
114   * Set to true if you want the BeanCollection to be treated as read only. This
115   * means no elements can be added or removed etc.
116   */
117  void setReadOnly(boolean readOnly);
118
119  /**
120   * Return true if the collection should be treated as readOnly and no elements
121   * can be added or removed etc.
122   */
123  boolean isReadOnly();
124
125  /**
126   * Add the bean to the collection.
127   * <p>
128   * This is disallowed for BeanMap.
129   * </p>
130   */
131  void internalAdd(Object bean);
132
133  /**
134   * Add the bean with a check to see if it is already contained.
135   */
136  void internalAddWithCheck(Object bean);
137
138  /**
139   * Return the number of elements in the List Set or Map.
140   */
141  int size();
142
143  /**
144   * Return true if the List Set or Map is empty.
145   */
146  boolean isEmpty();
147
148  /**
149   * Returns the underlying collection of beans from the Set, Map or List.
150   */
151  Collection<E> getActualDetails();
152
153  /**
154   * Returns the underlying entries so for Maps this is a collection of
155   * Map.Entry.
156   * <p>
157   * For maps this returns the entrySet as we need the keys of the map.
158   * </p>
159   */
160  Collection<?> getActualEntries();
161
162  /**
163   * return true if there are real rows held. Return false is this is using
164   * Deferred fetch to lazy load the rows and the rows have not yet been
165   * fetched.
166   */
167  boolean isPopulated();
168
169  /**
170   * Return true if this is a reference (lazy loading) bean collection. This is
171   * the same as !isPopulated();
172   */
173  boolean isReference();
174
175  /**
176   * Set modify listening on or off. This is used to keep track of objects that
177   * have been added to or removed from the list set or map.
178   * <p>
179   * This is required only for ManyToMany collections. The additions and
180   * deletions are used to insert or delete entries from the intersection table.
181   * Otherwise modifyListening is false.
182   * </p>
183   */
184  void setModifyListening(ModifyListenMode modifyListenMode);
185
186  /**
187   * Add an object to the additions list.
188   * <p>
189   * This will potentially end up as an insert into a intersection table for a
190   * ManyToMany.
191   * </p>
192   */
193  void modifyAddition(E bean);
194
195  /**
196   * Add an object to the deletions list.
197   * <p>
198   * This will potentially end up as an delete from an intersection table for a
199   * ManyToMany.
200   * </p>
201   */
202  void modifyRemoval(Object bean);
203
204  /**
205   * Return the list of objects added to the list set or map. These will used to
206   * insert rows into the intersection table of a ManyToMany.
207   */
208  Set<E> getModifyAdditions();
209
210  /**
211   * Return the list of objects removed from the list set or map. These will
212   * used to delete rows from the intersection table of a ManyToMany.
213   */
214  Set<E> getModifyRemovals();
215
216  /**
217   * Reset the set of additions and deletions. This is called after the
218   * additions and removals have been processed.
219   */
220  void modifyReset();
221}