Author: athimel Date: 2014-05-23 14:07:39 +0200 (Fri, 23 May 2014) New Revision: 3133 Url: http://forge.nuiton.org/projects/topia/repository/revisions/3133 Log: Replace some <K> generics where <O> fits better Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/internal/AbstractTopiaDao.java Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/internal/AbstractTopiaDao.java =================================================================== --- trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/internal/AbstractTopiaDao.java 2014-05-23 10:29:45 UTC (rev 3132) +++ trunk/topia-persistence/src/main/java/org/nuiton/topia/persistence/internal/AbstractTopiaDao.java 2014-05-23 12:07:39 UTC (rev 3133) @@ -451,111 +451,111 @@ return result; } - protected <K> K findUnique(String hql, Map<String, Object> hqlParameters) throws TopiaNoResultException, TopiaNonUniqueResultException { - K result = findUniqueOrNull(hql, hqlParameters); + protected <O> O findUnique(String hql, Map<String, Object> hqlParameters) throws TopiaNoResultException, TopiaNonUniqueResultException { + O result = findUniqueOrNull(hql, hqlParameters); if (result == null) { throw new TopiaNoResultException(hql, hqlParameters); } return result; } - protected <K> Optional<K> tryFindUnique(String hql, Map<String, Object> hqlParameters) throws TopiaNonUniqueResultException { - K uniqueOrNull = findUniqueOrNull(hql, hqlParameters); - Optional<K> result = Optional.fromNullable(uniqueOrNull); + protected <O> Optional<O> tryFindUnique(String hql, Map<String, Object> hqlParameters) throws TopiaNonUniqueResultException { + O uniqueOrNull = findUniqueOrNull(hql, hqlParameters); + Optional<O> result = Optional.fromNullable(uniqueOrNull); return result; } - protected <K> K findUniqueOrNull(String hql, Map<String, Object> hqlParameters) throws TopiaNonUniqueResultException { - List<K> results = find(hql, hqlParameters, 0, 1); + protected <O> O findUniqueOrNull(String hql, Map<String, Object> hqlParameters) throws TopiaNonUniqueResultException { + List<O> results = find(hql, hqlParameters, 0, 1); // If there is more than 1 result, throw an exception if (results.size() > 1) { throw new TopiaNonUniqueResultException(hql, hqlParameters); } // otherwise return the first one, or null - K result = Iterables.getOnlyElement(results, null); + O result = Iterables.getOnlyElement(results, null); return result; } - protected <K> K findFirst(String hql, Map<String, Object> hqlParameters) throws QueryMissingOrderException { - K result = findFirstOrNull(hql, hqlParameters); + protected <O> O findFirst(String hql, Map<String, Object> hqlParameters) throws QueryMissingOrderException { + O result = findFirstOrNull(hql, hqlParameters); if (result == null) { throw new TopiaNoResultException(hql, hqlParameters); } return result; } - protected <K> Optional<K> tryFindFirst(String hql, Map<String, Object> hqlParameters) throws QueryMissingOrderException { - K firstOrNull = findFirstOrNull(hql, hqlParameters); - Optional<K> result = Optional.fromNullable(firstOrNull); + protected <O> Optional<O> tryFindFirst(String hql, Map<String, Object> hqlParameters) throws QueryMissingOrderException { + O firstOrNull = findFirstOrNull(hql, hqlParameters); + Optional<O> result = Optional.fromNullable(firstOrNull); return result; } - protected <K> K findFirstOrNull(String hql, Map<String, Object> hqlParameters) throws QueryMissingOrderException { + protected <O> O findFirstOrNull(String hql, Map<String, Object> hqlParameters) throws QueryMissingOrderException { if (!hqlContainsOrderBy(hql)) { throw new QueryMissingOrderException(hql, hqlParameters); } - K result = findAnyOrNull(hql, hqlParameters); + O result = findAnyOrNull(hql, hqlParameters); return result; } - protected <K> K findAny(String hql, Map<String, Object> hqlParameters) throws TopiaNoResultException { - K result = findAnyOrNull(hql, hqlParameters); + protected <O> O findAny(String hql, Map<String, Object> hqlParameters) throws TopiaNoResultException { + O result = findAnyOrNull(hql, hqlParameters); if (result == null) { throw new TopiaNoResultException(hql, hqlParameters); } return result; } - protected <K> Optional<K> tryFindAny(String hql, Map<String, Object> hqlParameters) { - K anyOrNull = findAnyOrNull(hql, hqlParameters); - Optional<K> result = Optional.fromNullable(anyOrNull); + protected <O> Optional<O> tryFindAny(String hql, Map<String, Object> hqlParameters) { + O anyOrNull = findAnyOrNull(hql, hqlParameters); + Optional<O> result = Optional.fromNullable(anyOrNull); return result; } - protected <K> K findAnyOrNull(String hql) { + protected <O> O findAnyOrNull(String hql) { Preconditions.checkNotNull(hql); Map<String, Object> hqlParameters = Collections.emptyMap(); - K result = findAnyOrNull(hql, hqlParameters); + O result = findAnyOrNull(hql, hqlParameters); return result; } - protected <K> K findAnyOrNull(String hql, Map<String, Object> hqlParameters) { + protected <O> O findAnyOrNull(String hql, Map<String, Object> hqlParameters) { Preconditions.checkNotNull(hql); Preconditions.checkNotNull(hqlParameters); - List<K> results = find(hql, hqlParameters, 0, 0); - K result = Iterables.getOnlyElement(results, null); + List<O> results = find(hql, hqlParameters, 0, 0); + O result = Iterables.getOnlyElement(results, null); return result; } - protected <K> List<K> findAll(String hql) { + protected <O> List<O> findAll(String hql) { Preconditions.checkNotNull(hql); Map<String, Object> hqlParameters = Collections.emptyMap(); - List<K> result = findAll(hql, hqlParameters); + List<O> result = findAll(hql, hqlParameters); return result; } - protected <K> List<K> findAll(String hql, Map<String, Object> hqlParameters) { + protected <O> List<O> findAll(String hql, Map<String, Object> hqlParameters) { Preconditions.checkNotNull(hql); Preconditions.checkNotNull(hqlParameters); - List<K> result = topiaJpaSupport.findAll(hql, hqlParameters); + List<O> result = topiaJpaSupport.findAll(hql, hqlParameters); return result; } - protected <K> List<K> find(String hql, int startIndex, int endIndex) { + protected <O> List<O> find(String hql, int startIndex, int endIndex) { Preconditions.checkNotNull(hql); Map<String, Object> hqlParameters = Collections.emptyMap(); - List<K> result = find(hql, hqlParameters, startIndex, endIndex); + List<O> result = find(hql, hqlParameters, startIndex, endIndex); return result; } - protected <K> List<K> find(String hql, Map<String, Object> hqlParameters, int startIndex, int endIndex) { + protected <O> List<O> find(String hql, Map<String, Object> hqlParameters, int startIndex, int endIndex) { Preconditions.checkNotNull(hql); Preconditions.checkNotNull(hqlParameters); - List<K> result = topiaJpaSupport.find(hql, startIndex, endIndex, hqlParameters); + List<O> result = topiaJpaSupport.find(hql, startIndex, endIndex, hqlParameters); return result; } - protected <K> List<K> find(String hql, Map<String, Object> hqlParameters, PaginationParameter page) { + protected <O> List<O> find(String hql, Map<String, Object> hqlParameters, PaginationParameter page) { Preconditions.checkNotNull(hql); Preconditions.checkNotNull(hqlParameters); Preconditions.checkNotNull(page); @@ -572,7 +572,7 @@ hql += Joiner.on(", ").join(orderClauses); } - List<K> result = topiaJpaSupport.find( + List<O> result = topiaJpaSupport.find( hql, (int) page.getStartIndex(), (int) page.getEndIndex(), @@ -581,37 +581,37 @@ return result; } - protected <K> Iterable<K> findAllLazy(String hql) { + protected <O> Iterable<O> findAllLazy(String hql) { Map<String, Object> hqlParameters = Collections.emptyMap(); - Iterable<K> result= findAllLazy(hql, hqlParameters); + Iterable<O> result= findAllLazy(hql, hqlParameters); return result; } - protected <K> Iterable<K> findAllLazy(String hql, Map<String, Object> hqlParameters) { + protected <O> Iterable<O> findAllLazy(String hql, Map<String, Object> hqlParameters) { - Iterable<K> result= findAllLazy(hql, hqlParameters, batchSize); + Iterable<O> result= findAllLazy(hql, hqlParameters, batchSize); return result; } - protected <K> Iterable<K> findAllLazy(String hql, int batchSize) { + protected <O> Iterable<O> findAllLazy(String hql, int batchSize) { Map<String, Object> hqlParameters = Collections.emptyMap(); - Iterable<K> result = findAllLazy(hql, hqlParameters, batchSize); + Iterable<O> result = findAllLazy(hql, hqlParameters, batchSize); return result; } - protected <K> Iterable<K> findAllLazy(String hql, Map<String, Object> hqlParameters, int batchSize) { + protected <O> Iterable<O> findAllLazy(String hql, Map<String, Object> hqlParameters, int batchSize) { Preconditions.checkNotNull(hql); Preconditions.checkNotNull(hqlParameters); - final Iterator<K> iterator = new FindAllIterator<E, K>(this, + final Iterator<O> iterator = new FindAllIterator<E, O>(this, batchSize, hql, hqlParameters); - Iterable<K> result = new Iterable<K>() { + Iterable<O> result = new Iterable<O>() { @Override - public Iterator<K> iterator() { + public Iterator<O> iterator() { return iterator; } }; @@ -715,9 +715,9 @@ // return meta; // } - public static class FindAllIterator<E extends TopiaEntity, K> implements Iterator<K> { + public static class FindAllIterator<E extends TopiaEntity, O> implements Iterator<O> { - protected Iterator<K> data; + protected Iterator<O> data; protected final AbstractTopiaDao<E> dao; @@ -755,7 +755,7 @@ return result; } - public K next() { + public O next() { if (!hasNext()) { throw new NoSuchElementException(); } @@ -773,7 +773,7 @@ } // load new window of data - List<K> values = dao.find(hql, params, pageToLoad); + List<O> values = dao.find(hql, params, pageToLoad); data = values.iterator(); // Create a new pager on the next page @@ -781,7 +781,7 @@ } - K next = data.next(); + O next = data.next(); return next; }