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   * Set the disableLazyLoad state.
036   */
037  void setDisableLazyLoad(boolean disableLazyLoad);
038
039  /**
040   * Load bean from another collection.
041   */
042  void loadFrom(BeanCollection<?> other);
043
044  /**
045   * Add a bean to the list/set with modifyListen notification.
046   */
047  void addBean(E bean);
048
049  /**
050   * Remove a bean to the list/set with modifyListen notification.
051   */
052  void removeBean(E bean);
053
054  /**
055   * Reset the collection back to an empty state ready for reloading.
056   * <p>
057   * This is done as part of bean refresh.
058   */
059  void reset(EntityBean ownerBean, String propertyName);
060
061  /**
062   * Return true if the collection is uninitialised or is empty without any held modifications.
063   * <p>
064   * Returning true means can safely skip cascade save for this bean collection.
065   * </p>
066   */
067  boolean isSkipSave();
068
069  /**
070   * Return the bean that owns this collection.
071   */
072  EntityBean getOwnerBean();
073
074  /**
075   * Return the bean property name this collection represents.
076   */
077  String getPropertyName();
078
079  /**
080   * Check after the lazy load that the underlying collection is not null
081   * (handle case where join to many not outer).
082   * <p>
083   * That is, if the collection was not loaded due to filterMany predicates etc
084   * then make sure the collection is set to empty.
085   * </p>
086   */
087  boolean checkEmptyLazyLoad();
088
089  /**
090   * Return the filter (if any) that was used in building this collection.
091   * <p>
092   * This is so that the filter can be applied on refresh.
093   * </p>
094   */
095  ExpressionList<?> getFilterMany();
096
097  /**
098   * Set the filter that was used in building this collection.
099   */
100  void setFilterMany(ExpressionList<?> filterMany);
101
102  /**
103   * Return true if the collection has been registered with the batch loading context.
104   */
105  boolean isRegisteredWithLoadContext();
106
107  /**
108   * Set the loader that will be used to lazy/query load this collection.
109   * <p>
110   * This is effectively the batch loading context this collection is registered with.
111   * </p>
112   */
113  void setLoader(BeanCollectionLoader beanLoader);
114
115  /**
116   * Set to true if you want the BeanCollection to be treated as read only. This
117   * means no elements can be added or removed etc.
118   */
119  void setReadOnly(boolean readOnly);
120
121  /**
122   * Return true if the collection should be treated as readOnly and no elements
123   * can be added or removed etc.
124   */
125  boolean isReadOnly();
126
127  /**
128   * Add the bean to the collection.
129   * <p>
130   * This is disallowed for BeanMap.
131   * </p>
132   */
133  void internalAdd(Object bean);
134
135  /**
136   * Add the bean with a check to see if it is already contained.
137   */
138  void internalAddWithCheck(Object bean);
139
140  /**
141   * Return the number of elements in the List Set or Map.
142   */
143  int size();
144
145  /**
146   * Return true if the List Set or Map is empty.
147   */
148  boolean isEmpty();
149
150  /**
151   * Returns the underlying collection of beans from the Set, Map or List.
152   */
153  Collection<E> getActualDetails();
154
155  /**
156   * Returns the underlying entries so for Maps this is a collection of
157   * Map.Entry.
158   * <p>
159   * For maps this returns the entrySet as we need the keys of the map.
160   * </p>
161   */
162  Collection<?> getActualEntries();
163
164  /**
165   * return true if there are real rows held. Return false is this is using
166   * Deferred fetch to lazy load the rows and the rows have not yet been
167   * fetched.
168   */
169  boolean isPopulated();
170
171  /**
172   * Return true if this is a reference (lazy loading) bean collection. This is
173   * the same as !isPopulated();
174   */
175  boolean isReference();
176
177  /**
178   * Set modify listening on or off. This is used to keep track of objects that
179   * have been added to or removed from the list set or map.
180   * <p>
181   * This is required only for ManyToMany collections. The additions and
182   * deletions are used to insert or delete entries from the intersection table.
183   * Otherwise modifyListening is false.
184   * </p>
185   */
186  void setModifyListening(ModifyListenMode modifyListenMode);
187
188  /**
189   * Add an object to the additions list.
190   * <p>
191   * This will potentially end up as an insert into a intersection table for a
192   * ManyToMany.
193   * </p>
194   */
195  void modifyAddition(E bean);
196
197  /**
198   * Add an object to the deletions list.
199   * <p>
200   * This will potentially end up as an delete from an intersection table for a
201   * ManyToMany.
202   * </p>
203   */
204  void modifyRemoval(Object bean);
205
206  /**
207   * Return the list of objects added to the list set or map. These will used to
208   * insert rows into the intersection table of a ManyToMany.
209   */
210  Set<E> getModifyAdditions();
211
212  /**
213   * Return the list of objects removed from the list set or map. These will
214   * used to delete rows from the intersection table of a ManyToMany.
215   */
216  Set<E> getModifyRemovals();
217
218  /**
219   * Reset the set of additions and deletions. This is called after the
220   * additions and removals have been processed.
221   */
222  void modifyReset();
223}