Skip to content

suggestions for improvement 3 #34

@abdelgadir

Description

@abdelgadir

here are the implementations of YPage, YFetch and related classes.

public class YFetch implements Serializable {
/**
* The maximum depth of relations to traverse when eager fetching. Use -1 for no limit which is the default
* for openJpa.
*/
private int maxFetchDepth = -1;
private Multimap<Class, String> fetchProps = null;

private YFetch() {
    this(null, null);
}

/**
 * @param clazz     the class which <strong>directly</strong> defines the given fieldName
 * @param fieldName the fieldName to eagerly fetch
 */
private YFetch(Class clazz, String fieldName) {
    this.fetchProps = HashMultimap.create();
    if (clazz != null && fieldName != null) {
        this.fetchProps.put(clazz, fieldName);
    }
}

public static YFetch with(Class clazz, String fieldName) {
    return new YFetch(clazz, fieldName);
}

public static YFetch with() {
    return new YFetch();
}

public YFetch and(Class clazz, String fieldName) {
    this.fetchProps.put(clazz, fieldName);
    return this;
}

public YFetch and(Class clazz, String... fieldNames) {
    this.fetchProps.putAll(clazz, Lists.newArrayList(fieldNames));
    return this;
}

public YFetch and(Class clazz, Collection<String> fieldNames) {
    this.fetchProps.putAll(clazz, fieldNames);
    return this;
}

public YFetch setMaxFetchDepth(int maxFetchDepth) {
    this.maxFetchDepth = maxFetchDepth;
    return this;
}

public int getMaxFetchDepth() {
    return maxFetchDepth;
}

public Map<Class, Collection<String>> getFetchProperties() {
    return fetchProps.asMap();
}

public boolean has(Class clazz, String fieldName) {
    return fetchProps.containsEntry(clazz, fieldName);
}

}

/**

  • DAO meta class for paging and sorting.
    *

  • @author altuure
    /
    public class YLimit implements Serializable {
    private static final long serialVersionUID = 1L;
    /
    *

    • not assigned value.
      /
      protected static final int NOT_ASSIGNED = -1;
      /
      *
    • index of first result.
      /
      protected int firstResultIndex = NOT_ASSIGNED;
      /
      *
    • result set page size. The page size specifies the maximum number of records to retrieve
      */
      protected int pageSize = NOT_ASSIGNED;

    /**

    • limit result with given properties.
      *
    • @param firstResultIndex first result index
    • @param pageSize page size
      */
      public YLimit(int firstResultIndex, int pageSize) {
      this.firstResultIndex = firstResultIndex;
      this.pageSize = pageSize;
      }

    /**

    • unlimited result size from given index.
      *
    • @param firstResultIndex first result index
      */
      public YLimit(int firstResultIndex) {
      this(firstResultIndex, NOT_ASSIGNED);
      }

    /**

    • default cons.
      */
      public YLimit() {
      this(NOT_ASSIGNED, NOT_ASSIGNED);
      }

    /**

    • first result index.
      *
    • @return first result
      */
      public int getFirstResultIndex() {
      return firstResultIndex;
      }

    private void setFirstResultIndex(int firstResultIndex) {
    this.firstResultIndex = firstResultIndex;
    }

    /**

    • This method works out the firstResultIndex based on given pageNumber and pageSize. The given pageSize will
    • replace the existing pageSize if present.
    • @param pageNumber page to fetch
    • @param zeroBasedPaging boolean indicating whether paging starts at zero
      */
      public void setPageNumber(int pageNumber, int pageSize, boolean zeroBasedPaging){
      setPageSize(pageSize);
      setFirstResultIndex(zeroBasedPaging? (pageNumber * pageSize): ((pageNumber-1) * pageSize));
      }

    /**

    • @return page size.
      */
      public int getPageSize() {
      return pageSize;
      }

    private void setPageSize(int pageSize) {
    this.pageSize = pageSize;
    }

    /**

    • get whether this limit object represents a page as opposed to the total results.
      *
    • @return whether firstResultIndex>0
      */
      public boolean isPaged() {
      return firstResultIndex >= 0;
      }

    /**

    • get whether the page size is defined.
      *
    • @return pageSize>0
      */
      public boolean isPageSized() {
      // return pageSize >= 0;
      return pageSize > 0;
      }

    @OverRide
    public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    YLimit yLimit = (YLimit) o;
    
    if (firstResultIndex != yLimit.firstResultIndex) return false;
    if (pageSize != yLimit.pageSize) return false;
    
    return true;
    

    }

    @OverRide
    public int hashCode() {
    int result = firstResultIndex;
    result = 31 * result + pageSize;
    return result;
    }

    /*

    • (non-Javadoc)
      *
    • @see java.lang.Object#toString()
      */
      @OverRide
      public String toString() {
      return "YLimit [firstResultIndex=" + firstResultIndex + ", pageSize=" + pageSize + "]";
      }
      }

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;

import java.io.Serializable;

/**

  • Class used to specify a sorted property
    *

  • @author altuure

  • @author abdelgadir ibrahim
    */
    public class YOrder implements Serializable {
    private static final long serialVersionUID = 1L;

    /**

    • Available sort orders
      */
      public enum Sort {
      ASC, DESC
      }

    /**

    • sort field name
      */
      private String name;

    /**

    • sort order
      */
      private Sort sort;

    /**

    • construct a YOrder to sort given property in asc order. The property name is a path expression starting from
    • the returned root entity excluding the root itself. For example, assuming property 'Employee.user.firstName',
    • where Employee is the returned root class, it should be specified as 'user.firstName'.
      *
    • @param name sort property name. Assumes asc sort order
      */
      public YOrder(String name) {
      this(name, Sort.ASC);
      }

    /**

    • construct a YOrder to sort given property by the given sort order. The property name is a path expression
    • starting from the returned root entity excluding the root itself. For example, assuming property
    • 'Employee.user.firstName', where Employee is the returned root class, it should be specified as 'user.firstName'.
      *
    • @param name sort property
    • @param sort sort order
      */
      public YOrder(String name, Sort sort) {
      super();
      Preconditions.checkNotNull(Strings.emptyToNull(name), "sort property must not be null or empty");
      this.name = name;
      this.sort = sort;
      }

    /**

    • get the sort property name
    • @return sort property name
      */
      public String getName() {
      return name;
      }

    /**

    • get sort order
      *
    • @return sort order
      */
      public Sort getSort() {
      return sort;
      }

    @OverRide
    public String toString() {
    return "YOrder{" +
    "name='" + name + ''' +
    ", sort=" + sort +
    '}';
    }

    @OverRide
    public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    result = prime * result + ((sort == null) ? 0 : sort.hashCode());
    return result;
    }

    @OverRide
    public boolean equals(Object obj) {
    if (this == obj)
    return true;
    if (obj == null)
    return false;
    if (getClass() != obj.getClass())
    return false;
    YOrder other = (YOrder) obj;
    if (name == null) {
    if (other.name != null)
    return false;
    } else if (!name.equals(other.name))
    return false;
    if (sort != other.sort)
    return false;
    return true;
    }
    }

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class YPage extends YLimit implements Serializable {
/**
* serialization key.
/
private static final long serialVersionUID = 1L;
/
*
* order sort list.
*/
private List orders = new ArrayList(2);

/**
 * dao page serialization constructor.
 */
public YPage() {
    super();
}

/**
 * dao page serialization constructor.
 *
 * @param limit limits
 */
public static YPage copy(YLimit limit) {
    if (limit == null) {
        return new YPage();
    } else {
        return new YPage(limit.firstResultIndex, limit.pageSize);
    }
}

/**
 * order by given property asc. The property name is a path expression starting from
 * the returned root entity excluding the root itself. For example, assuming property 'Employee.user.firstName',
 * where Employee is the returned root class, it should be specified as 'user.firstName'.
 *
 * @param orderBy property asc.
 */
public YPage(String orderBy) {
    this(orderBy, true, NOT_ASSIGNED, NOT_ASSIGNED);
}

/**
 * order by given property. The orderBy property name is a path expression starting from
 * the returned root entity excluding the root itself. For example, assuming property 'Employee.user.firstName',
 * where Employee is the returned root class, it should be specified as 'user.firstName'.
 *
 * @param orderBy   property .
 * @param ascending is asc
 */
public YPage(String orderBy, boolean ascending) {
    this(orderBy, ascending, NOT_ASSIGNED, NOT_ASSIGNED);
}

/**
 * dao page. The orderBy property name is a path expression starting from
 * the returned root entity excluding the root itself. For example, assuming property 'Employee.user.firstName',
 * where Employee is the returned root class, it should be specified as 'user.firstName'.
 *
 * @param orderBy     order property
 * @param ascending   asc
 * @param firstResultIndex first result index
 * @param pageSize    result size
 */
public YPage(String orderBy, boolean ascending, int firstResultIndex, int pageSize) {
    super(firstResultIndex, pageSize);
    if (orderBy != null) {
        orders.add(new YOrder(orderBy, (ascending) ? YOrder.Sort.ASC : YOrder.Sort.DESC));
    }
}

/**
 * limit result with given properties.
 *
 * @param firstResultIndex first result index
 * @param pageSize    page size
 */
public YPage(int firstResultIndex, int pageSize) {
    this(null, false, firstResultIndex, pageSize);
}

/**
 * unlimited result size from given index.
 *
 * @param firstResultIndex first result index
 */
public YPage(int firstResultIndex) {
    this(null, false, firstResultIndex, NOT_ASSIGNED);
}

/**
 * is ordered.
 *
 * @return is ordered
 */
public boolean isOrdered() {
    return orders != null && !orders.isEmpty();
}

/**
 * util method to add order.The orderBy property name is a path expression starting from
 * the returned root entity excluding the root itself. For example, assuming property 'Employee.user.firstName',
 * where Employee is the returned root class, it should be specified as 'user.firstName'.
 *
 * @param orderBy   order property.
 * @param ascending sort order.
 * @return this
 */
public YPage addOrder(String orderBy, boolean ascending) {
    orders.add(new YOrder(orderBy, (ascending) ? YOrder.Sort.ASC : YOrder.Sort.DESC));
    return this;
}

/**
 * builder method to sort result set. The orderBy property name is a path expression starting from
 * the returned root entity excluding the root itself. For example, assuming property 'Employee.user.firstName',
 * where Employee is the returned root class, it should be specified as 'user.firstName'.
 *
 * @param orderBy propery to sort
 * @param sort    sort order
 * @return this
 */
public YPage addOrder(String orderBy, YOrder.Sort sort) {
    orders.add(new YOrder(orderBy, sort));
    return this;
}

/**
 * list of orders.
 *
 * @return orders
 */
public List<YOrder> getOrders() {
    return orders;
}

/**
 * orders to set.
 *
 * @param orders orders
 */
public void setOrders(List<YOrder> orders) {
    this.orders = orders;
}

@Override
public int hashCode() {
    int result = super.hashCode();
    final int prime = 31;
    result = prime * result + ((orders == null) ? 0 : orders.hashCode());

    return result;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof YPage)) return false;
    if (!super.equals(o)) return false;

    YPage yPage = (YPage) o;

    if (orders != null ? !orders.equals(yPage.orders) : yPage.orders != null) return false;

    return true;
}

@Override
public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("YPage [orders=");
    builder.append(orders);
    builder.append(", YLimit=");
    builder.append(super.toString());
    builder.append("]");
    return builder.toString();
}

}

import com.google.common.base.Preconditions;
import com.google.common.collect.ForwardingList;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**

  • List encapsulating a result set with details of total result size and paging details.
    /
    public class SearchResultList extends ForwardingList implements Serializable {
    /
    *

    • Paging details. A search result could be paged in the sense that it represents a single page of the total
    • retrievable results. In such case, the paging variable describes which page is being returned.
    • The paging index is zero-based.
      /
      private YPage paging;
      /
      *
    • total number of results that would have been returned if no maxResults had been specified.
    • In other words, it is the total retrievable result size; not just for a single page but for all possible pages.
    • This total count may or may not match the number of actual results being returned by this search result instance.
      /
      private long totalCount;
      /
      *
    • decorated list containing the results being returned. This list doesn't necessarily represent the total
    • retrievable results, for example, when the results are being returned in junks/pages
      */
      private List delegate;

    public SearchResultList() {
    this(Collections.emptyList());
    }

    public SearchResultList(List delegate) {
    this(delegate, null);
    }

    /**

    • non-paged search result list i.e., the results being returned by this instance represent all retrievable results.
    • In this case the totalCount==result.size()
      */
      public SearchResultList(List result, YLimit limit) {
      this(result, result==null? 0 : result.size(), YPage.copy(limit));
      }

    /**

    • paged search result list i.e., the set of results being returned by this instance represent the content of only
    • one page. Total count is the total retrievable across all pages.
    • In other words, 'the results in this instance (which are stored in delegate) represent only one page
    • (as defined by paging) of the total set of retrievable results which is equals to totalCount'.
      */
      public SearchResultList(List delegate, long totalCount, YPage paging) {
      super();
      Preconditions.checkNotNull(delegate, "delegate must not be null");
      this.delegate = delegate;
      this.totalCount = totalCount;
      this.paging = paging;
      }

    @OverRide
    protected List delegate() {
    return delegate;
    }

    public YPage getPaging() {
    return paging;
    }

    public long getTotalCount() {
    return totalCount;
    }

    /**

    • get the index of this result's page.
      *@param zeroBasedPaging provided by caller to indicates whether it uses zeroBased page numbering
    • @return index of the page defined by this result
      */
      public int getPageIndex(boolean zeroBasedPaging) {
      int result = (int) paging.getFirstResultIndex() / paging.getPageSize();
      return zeroBasedPaging? result : result+1;
      }

    /**

    • @return page count
      */
      public int getPageCount() {
      //is i the lastPageIndex ??!!
      int i = (int) totalCount / paging.getPageSize();
      if (totalCount % paging.getPageSize() != 0) {
      i = i + 1;
      }
      return i;
      }

    public boolean hasPreviousPage(boolean zeroBasedPaging){
    return getPageIndex(zeroBasedPaging) > 0;
    }

    public boolean hasNextPage(boolean zeroBasedPaging){
    return ((getPageIndex(zeroBasedPaging)+1) * getPaging().getPageSize()) < getTotalCount();
    }

    @OverRide
    public String toString() {
    return "SearchResultList{" +
    "paging=" + paging +
    ", totalCount=" + totalCount +
    '}';
    }

    @OverRide
    public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    if (!super.equals(o)) return false;

    SearchResultList that = (SearchResultList) o;
    
    if (totalCount != that.totalCount) return false;
    if (paging != null ? !paging.equals(that.paging) : that.paging != null) return false;
    
    return true;
    

    }

    @OverRide
    public int hashCode() {
    int result = super.hashCode();
    result = 31 * result + (paging != null ? paging.hashCode() : 0);
    result = 31 * result + (int) (totalCount ^ (totalCount >>> 32));
    return result;
    }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions