Commits

Vadim Ne. committed 8c11216

Task 3. Multiple constructors rewritten to query objects. Code cleanup.

  • Participants
  • Parent commits 0ca923a

Comments (0)

Files changed (22)

database-services-api/src/main/java/queryConverters/NgramsListQueryObjectToDaoQueryObjectConverter.java

+package queryConverters;
+
+import dao.daoQueryObjects.NgramsListDaoQueryObject;
+import queryObjects.NgramsListQueryObject;
+
+public class NgramsListQueryObjectToDaoQueryObjectConverter {
+    public static NgramsListDaoQueryObject convert(NgramsListQueryObject ngramsListQuery) {
+        return new NgramsListDaoQueryObject(
+                ngramsListQuery.getAmount(),
+                ngramsListQuery.getOffsetPage(),
+                ngramsListQuery.getOrderByFieldName(),
+                ngramsListQuery.getOrder());
+    }
+}

database-services-api/src/main/java/queryConverters/WordsListQueryObjectToDaoQueryObjectConverter.java

+package queryConverters;
+
+import dao.daoQueryObjects.WordsListDaoQueryObject;
+import queryObjects.WordsListQueryObject;
+
+public class WordsListQueryObjectToDaoQueryObjectConverter {
+    public static WordsListDaoQueryObject convert(int ngramId, WordsListQueryObject wordsListQuery) {
+        return new WordsListDaoQueryObject(ngramId,
+                wordsListQuery.getOffsetPage(),
+                wordsListQuery.getAmountOfElements(),
+                wordsListQuery.getWordToSearchPattern(),
+                wordsListQuery.getOrderByFieldName(),
+                wordsListQuery.getOrder());
+    }
+}

database-services-api/src/main/java/queryObjects/NgramsListQueryObject.java

+package queryObjects;
+
+import queryOptions.NgramsSortFieldName;
+import queryOptions.Order;
+
+public class NgramsListQueryObject {
+    public static class NgramsListQueryBuilder {
+        //required
+        private int amount;
+        private int offsetPage;
+
+        //optional
+        private String orderByFieldNameString;
+        private NgramsSortFieldName orderByFieldName;
+        private String orderString;
+        private Order order;
+
+        public NgramsListQueryBuilder(int amount, int offsetPage) {
+            this.amount = amount;
+            this.offsetPage = offsetPage;
+        }
+
+        public NgramsListQueryBuilder orderByField(Enum fieldName) {
+            if (fieldName == null)
+                this.orderByFieldNameString = "";
+            else
+                this.orderByFieldNameString = fieldName.name();
+            return this;
+        }
+
+        public NgramsListQueryBuilder order(Enum order) {
+            if (order == null)
+                this.orderString = "";
+            else
+                this.orderString = order.name();
+            return this;
+        }
+
+        public NgramsListQueryObject build() {
+            if (amount < 1) {
+                throw new IllegalStateException("Amount of items in list is negative or zero.");
+            }
+
+            if (offsetPage < 0) {
+                throw new IllegalStateException("Offset page number is negative.");
+            }
+
+            try {
+                orderByFieldName = NgramsSortFieldName.valueOf(orderByFieldNameString);
+            } catch (Exception e) {
+                orderByFieldName = null;
+                //throw new IllegalStateException("Field name to order is incorrect.");
+            }
+
+            try {
+                order = Order.valueOf(orderString);
+            } catch (Exception e) {
+                order = null;
+                //throw new IllegalStateException("Order tag is incorrect.");
+            }
+
+            return new NgramsListQueryObject(this);
+        }
+    }
+
+    //required
+    private final int amount;
+    private final int offsetPage;
+
+    //optional
+    private final NgramsSortFieldName orderByFieldName;
+    private final Order order;
+
+    private NgramsListQueryObject(NgramsListQueryBuilder ngramsListQueryBuilder) {
+        this.amount = ngramsListQueryBuilder.amount;
+        this.offsetPage = ngramsListQueryBuilder.offsetPage;
+        this.orderByFieldName = ngramsListQueryBuilder.orderByFieldName;
+        this.order = ngramsListQueryBuilder.order;
+    }
+
+    public int getAmount() {
+        return amount;
+    }
+
+    public int getOffsetPage() {
+        return offsetPage;
+    }
+
+    public NgramsSortFieldName getOrderByFieldName() {
+        return orderByFieldName;
+    }
+
+    public Order getOrder() {
+        return order;
+    }
+}

database-services-api/src/main/java/queryObjects/WordsListQueryObject.java

+package queryObjects;
+
+import org.apache.commons.lang3.StringUtils;
+import queryOptions.Order;
+import queryOptions.WordsSortFieldName;
+
+public class WordsListQueryObject {
+    public static class WordsListQueryBuilder {
+        //required
+        private String ngramData;
+        private int amountOfElements;
+        private int offsetPage;
+        private String wordToSearchPattern;
+
+        //optional
+        private String orderByFieldNameString;
+        private WordsSortFieldName orderByFieldName;
+        private String orderString;
+        private Order order;
+
+        public WordsListQueryBuilder(String ngramData, int amountOfElements, int offsetPage, String wordToSearchPattern) {
+            this.ngramData = ngramData;
+            this.amountOfElements = amountOfElements;
+            this.offsetPage = offsetPage;
+            this.wordToSearchPattern = wordToSearchPattern;
+        }
+
+        public WordsListQueryBuilder orderByField(Enum fieldName) {
+            if (fieldName == null)
+                this.orderByFieldNameString = "";
+            else
+                this.orderByFieldNameString = fieldName.name();
+            return this;
+        }
+
+        public WordsListQueryBuilder order(Enum order) {
+            if (order == null)
+                this.orderString = "";
+            else
+                this.orderString = order.name();
+            return this;
+        }
+
+        public WordsListQueryObject build() {
+            if (StringUtils.isEmpty(ngramData)) {
+                throw new IllegalStateException("N-gram data is not defined.");
+            }
+
+            if (amountOfElements < 1) {
+                throw new IllegalStateException("Amount of items in list is negative or zero.");
+            }
+
+            if (offsetPage < 0) {
+                throw new IllegalStateException("Offset page number is negative.");
+            }
+
+            if (StringUtils.isEmpty(wordToSearchPattern)) {
+                throw new IllegalStateException("Word search pattern is null or empty.");
+            }
+
+            try {
+                orderByFieldName = WordsSortFieldName.valueOf(orderByFieldNameString);
+            } catch (Exception e) {
+                orderByFieldName = null;
+                //throw new IllegalStateException("Field name to order is incorrect.");
+            }
+
+            try {
+                order = Order.valueOf(orderString);
+            } catch (Exception e) {
+                order = null;
+                //throw new IllegalStateException("Order tag is incorrect.");
+            }
+
+            return new WordsListQueryObject(this);
+        }
+    }
+
+    //required
+    private final int amountOfElements;
+    private final int offsetPage;
+    private String ngramData;
+    private String wordToSearchPattern;
+
+    //optional
+    private final WordsSortFieldName orderByFieldName;
+    private final Order order;
+
+    private WordsListQueryObject(WordsListQueryBuilder wordsListQueryBuilder) {
+        this.amountOfElements = wordsListQueryBuilder.amountOfElements;
+        this.offsetPage = wordsListQueryBuilder.offsetPage;
+        this.ngramData = wordsListQueryBuilder.ngramData;
+        this.wordToSearchPattern = wordsListQueryBuilder.wordToSearchPattern;
+        this.orderByFieldName = wordsListQueryBuilder.orderByFieldName;
+        this.order = wordsListQueryBuilder.order;
+    }
+
+    public int getAmountOfElements() {
+        return amountOfElements;
+    }
+
+    public int getOffsetPage() {
+        return offsetPage;
+    }
+
+    public String getNgramData() {
+        return ngramData;
+    }
+
+    public String getWordToSearchPattern() {
+        return wordToSearchPattern;
+    }
+
+    public WordsSortFieldName getOrderByFieldName() {
+        return orderByFieldName;
+    }
+
+    public Order getOrder() {
+        return order;
+    }
+}

database-services-api/src/main/java/queryOptions/NgramsSortFieldName.java

+package queryOptions;
+
+public enum NgramsSortFieldName {
+    name, count
+}

database-services-api/src/main/java/queryOptions/Order.java

+package queryOptions;
+
+public enum Order {
+    asc, desc
+}

database-services-api/src/main/java/queryOptions/WordsSortFieldName.java

+package queryOptions;
+
+public enum WordsSortFieldName {
+    word, count
+}

database-services-api/src/main/java/services/NgramsDatabaseReaderInterface.java

 package services;
 
 import dao.entities.dbTables.Ngram;
-import dao.queryOptions.NgramsReaderFieldName;
-import dao.queryOptions.Order;
+import queryObjects.NgramsListQueryObject;
 
 import java.util.List;
 
 public interface NgramsDatabaseReaderInterface {
     Ngram searchNgramByDataFullMatch(String ngramData);
 
-    List<Ngram> getListOfNgrams(int offsetPage, int amountOfItems);
-
-    List<Ngram> getListOfNgrams(int offsetPage, int amountOfItems, NgramsReaderFieldName fieldName);
-
-    List<Ngram> getListOfNgrams(int offsetPage, int amountOfItems, NgramsReaderFieldName fieldName, Order order);
+    List<Ngram> getListOfNgrams(NgramsListQueryObject ngramsListQuery);
 
     int getAmountOfNgramsInDatabase();
 }

database-services-api/src/main/java/services/WordsDatabaseReaderInterface.java

 package services;
 
 import dao.entities.logical.WordForNgramWithCount;
-import dao.queryOptions.Order;
-import dao.queryOptions.WordsSortFieldName;
+import queryObjects.WordsListQueryObject;
 
 import java.util.List;
 
 public interface WordsDatabaseReaderInterface {
-    List<WordForNgramWithCount> getListOfWordsWithCounters(int offsetPage, int amountOfItems, String ngramData, String partOfWord);
-
-    List<WordForNgramWithCount> getListOfWordsWithCounters(int offsetPage, int amountOfItems, String ngramData, String partOfWord, WordsSortFieldName fieldName);
-
-    List<WordForNgramWithCount> getListOfWordsWithCounters(int offsetPage, int amountOfItems, String ngramData, String partOfWord, WordsSortFieldName fieldName, Order order);
+    List<WordForNgramWithCount> getListOfWordsWithCounters(WordsListQueryObject wordsListQuery);
 
     int getAmountOfWordsWithCountersInDatabase(String ngramData, String partOfWord);
 }

database-services-impl/src/main/java/services/NgramsJdbcReader.java

 package services;
 
 import dao.NgramsDao;
+import dao.daoQueryObjects.NgramsListDaoQueryObject;
 import dao.entities.dbTables.Ngram;
 import dao.jdbcDao.JdbcNgramsDao;
-import dao.queryOptions.NgramsReaderFieldName;
 import jdbcUtilities.JdbcH2DataSource;
-import dao.queryOptions.Order;
+import queryConverters.NgramsListQueryObjectToDaoQueryObjectConverter;
+import queryObjects.NgramsListQueryObject;
 
 import java.util.List;
 
     }
 
     @Override
-    public List<Ngram> getListOfNgrams(int offsetPage, int amountOfItems) {
-        if (offsetPage < 0 || amountOfItems < 1)
-            return null;
-        return ngramsDao.extractListOfItemsById(offsetPage, amountOfItems);
-    }
-
-    @Override
-    public List<Ngram> getListOfNgrams(int offsetPage, int amountOfItems, NgramsReaderFieldName fieldName) {
-        if (offsetPage < 0 || amountOfItems < 1 || fieldName == null)
-            return null;
-
-        if (fieldName == NgramsReaderFieldName.name) {
-            return ngramsDao.extractListOfItemsAscendByName(offsetPage, amountOfItems);
-        } else {
-            return ngramsDao.extractListOfItemsAscendByCount(offsetPage, amountOfItems);
-        }
-    }
-
-    @Override
-    public List<Ngram> getListOfNgrams(int offsetPage, int amountOfItems, NgramsReaderFieldName fieldName, Order order) {
-        if (offsetPage < 0 || amountOfItems < 1 || fieldName == null || order == null)
+    public List<Ngram> getListOfNgrams(NgramsListQueryObject ngramsListQuery) {
+        if (ngramsListQuery == null)
             return null;
 
-        if (fieldName == NgramsReaderFieldName.name) {
-            if (order == Order.asc) {
-                return ngramsDao.extractListOfItemsAscendByName(offsetPage, amountOfItems);
-            } else {
-                return ngramsDao.extractListOfItemsDescendByName(offsetPage, amountOfItems);
-            }
-        } else {
-            if (order == Order.asc) {
-                return ngramsDao.extractListOfItemsAscendByCount(offsetPage, amountOfItems);
-            } else {
-                return ngramsDao.extractListOfItemsDescendByCount(offsetPage, amountOfItems);
-            }
-        }
+        NgramsListDaoQueryObject ngramsDaoQO = NgramsListQueryObjectToDaoQueryObjectConverter.convert(ngramsListQuery);
+        return ngramsDao.extractListOfNgrams(ngramsDaoQO);
     }
 
     @Override

database-services-impl/src/main/java/services/WordsJdbcReader.java

 
 import dao.NgramsDao;
 import dao.WordsDao;
+import dao.daoQueryObjects.WordsListDaoQueryObject;
 import dao.entities.logical.WordForNgramWithCount;
 import dao.jdbcDao.JdbcNgramsDao;
 import dao.jdbcDao.JdbcWordsDao;
 import jdbcUtilities.JdbcH2DataSource;
 import org.apache.commons.lang3.StringUtils;
-import dao.queryOptions.Order;
-import dao.queryOptions.WordsSortFieldName;
+import queryConverters.WordsListQueryObjectToDaoQueryObjectConverter;
+import queryObjects.WordsListQueryObject;
 
 import java.sql.Connection;
 import java.util.List;
     }
 
     @Override
-    public List<WordForNgramWithCount> getListOfWordsWithCounters(int offsetPage, int amountOfItems, String ngramData, String partOfWord) {
-        if (offsetPage < 0 || StringUtils.isEmpty(ngramData) || StringUtils.isEmpty(partOfWord)) {
+    public List<WordForNgramWithCount> getListOfWordsWithCounters(WordsListQueryObject wordsListQuery) {
+        if (wordsListQuery == null)
             return null;
-        }
-        int ngramId = ngramsDao.getIdForNgram(ngramData);
-        if (ngramId > 0) {
-            return wordsDao.findAndExtractListOfWordsForNgramByItsDataPartialMatchSortById(partOfWord, ngramId, amountOfItems, offsetPage);
-        }
-        return null;
-    }
 
-    @Override
-    public List<WordForNgramWithCount> getListOfWordsWithCounters(int offsetPage, int amountOfItems, String ngramData, String partOfWord, WordsSortFieldName fieldName) {
-        if (offsetPage < 0 || StringUtils.isEmpty(ngramData) || StringUtils.isEmpty(partOfWord) || fieldName == null)
-            return null;
-
-        int ngramId = ngramsDao.getIdForNgram(ngramData);
-        if (ngramId > 0) {
-            if (fieldName == WordsSortFieldName.word) {
-                return wordsDao.findAndExtractListOfWordsForNgramByItsDataPartialMatchSortByNameAsc(partOfWord, ngramId, amountOfItems, offsetPage);
-            } else {
-                return wordsDao.findAndExtractListOfWordsForNgramByItsDataPartialMatchSortByCountAsc(partOfWord, ngramId, amountOfItems, offsetPage);
-            }
-        }
-        return null;
-    }
-
-    @Override
-    public List<WordForNgramWithCount> getListOfWordsWithCounters(int offsetPage, int amountOfItems, String ngramData, String partOfWord, WordsSortFieldName fieldName, Order order) {
-        if (offsetPage < 0 || StringUtils.isEmpty(ngramData) || StringUtils.isEmpty(partOfWord) || fieldName == null || order == null)
-            return null;
-
-        int ngramId = ngramsDao.getIdForNgram(ngramData);
-        if (ngramId > 0) {
-            if (fieldName == WordsSortFieldName.word) {
-                if (order == Order.asc) {
-                    return wordsDao.findAndExtractListOfWordsForNgramByItsDataPartialMatchSortByNameAsc(partOfWord, ngramId, amountOfItems, offsetPage);
-                } else {
-                    return wordsDao.findAndExtractListOfWordsForNgramByItsDataPartialMatchSortByNameDesc(partOfWord, ngramId, amountOfItems, offsetPage);
-                }
-            } else {
-                if (order == Order.asc) {
-                    return wordsDao.findAndExtractListOfWordsForNgramByItsDataPartialMatchSortByCountAsc(partOfWord, ngramId, amountOfItems, offsetPage);
-                } else {
-                    return wordsDao.findAndExtractListOfWordsForNgramByItsDataPartialMatchSortByCountDesc(partOfWord, ngramId, amountOfItems, offsetPage);
-                }
-            }
-        }
-        return null;
+        int ngramId = ngramsDao.getIdForNgram(wordsListQuery.getNgramData());
+        WordsListDaoQueryObject wrdsDaoQO = WordsListQueryObjectToDaoQueryObjectConverter.convert(ngramId, wordsListQuery);
+        return wordsDao.findAndExtractListOfWordsForNgramByPartialWordMatch(wrdsDaoQO);
     }
 
     @Override

libngram-database/src/main/java/dao/NgramsDao.java

 package dao;
 
+import dao.daoQueryObjects.NgramsListDaoQueryObject;
 import dao.entities.dbTables.Ngram;
-import extractedDataFormats.WordWithCountOfNgram;
 
 import java.util.List;
 
 
     int getIdForNgram(String ngramDataString);
 
-    List<Ngram> extractListOfItemsById(int offsetPage, int amountOfItems);
-
-    List<Ngram> extractListOfItemsAscendByName(int offsetPage, int amountOfItems);
-
-    List<Ngram> extractListOfItemsDescendByName(int offsetPage, int amountOfItems);
-
-    List<Ngram> extractListOfItemsAscendByCount(int offsetPage, int amountOfItems);
-
-    List<Ngram> extractListOfItemsDescendByCount(int offsetPage, int amountOfItems);
-
-    List<WordWithCountOfNgram> extractListOfWordsWithCountForNgramById(int ngramId, int offsetPage, int amountOfItems);
-
-    List<WordWithCountOfNgram> extractListOfWordsWithCountForNgramAscendByWordData(int ngramId, int offsetPage, int amountOfItems);
-
-    List<WordWithCountOfNgram> extractListOfWordsWithCountForNgramDescendByWordData(int ngramId, int offsetPage, int amountOfItems);
-
-    List<WordWithCountOfNgram> extractListOfWordsWithCountForNgramAscendByCount(int ngramId, int offsetPage, int amountOfItems);
-
-    List<WordWithCountOfNgram> extractListOfWordsWithCountForNgramDescendByCount(int ngramId, int offsetPage, int amountOfItems);
+    List<Ngram> extractListOfNgrams(NgramsListDaoQueryObject ngramsListDaoQueryObject);
 
     int extractAmountOfNgrams();
 

libngram-database/src/main/java/dao/WordsDao.java

 package dao;
 
+import dao.daoQueryObjects.WordsListDaoQueryObject;
 import dao.entities.logical.WordForNgramWithCount;
 
 import java.util.List;
 
     int findWord(String wordDataString);
 
-    List<WordForNgramWithCount> findAndExtractListOfWordsForNgramByItsDataPartialMatchSortById(String partOfWord, int ngramId, int amountOfItems, int pageOffset);
-
-    List<WordForNgramWithCount> findAndExtractListOfWordsForNgramByItsDataPartialMatchSortByNameAsc(String partOfWord, int ngramId, int amountOfItems, int pageOffset);
-
-    List<WordForNgramWithCount> findAndExtractListOfWordsForNgramByItsDataPartialMatchSortByNameDesc(String partOfWord, int ngramId, int amountOfItems, int pageOffset);
-
-    List<WordForNgramWithCount> findAndExtractListOfWordsForNgramByItsDataPartialMatchSortByCountAsc(String partOfWord, int ngramId, int amountOfItems, int pageOffset);
-
-    List<WordForNgramWithCount> findAndExtractListOfWordsForNgramByItsDataPartialMatchSortByCountDesc(String partOfWord, int ngramId, int amountOfItems, int pageOffset);
+    List<WordForNgramWithCount> findAndExtractListOfWordsForNgramByPartialWordMatch(WordsListDaoQueryObject wordsListDaoQueryObject);
 
     int extractAmountOfWordsForNgramByItsDataPartialMatch(int ngramData, String partOfWord);
 

libngram-database/src/main/java/dao/daoQueryObjects/NgramsListDaoQueryObject.java

+package dao.daoQueryObjects;
+
+import dao.queryOptions.NgramsSortFieldName;
+import dao.queryOptions.Order;
+
+public class NgramsListDaoQueryObject {
+    //required
+    private int amount;
+    private int offsetPage;
+
+    //optional
+    private NgramsSortFieldName orderByFieldName;
+    private Order order;
+
+    public NgramsListDaoQueryObject(int amount, int offsetPage, Enum orderByFieldName, Enum order) {
+        this.amount = amount;
+        this.offsetPage = offsetPage;
+
+        if (orderByFieldName == null)
+            this.orderByFieldName = null;
+        else
+            this.orderByFieldName = NgramsSortFieldName.valueOf(orderByFieldName.name());
+
+        if (order == null)
+            this.order = null;
+        else
+            this.order = Order.valueOf(order.name());
+    }
+
+    public int getAmount() {
+        return amount;
+    }
+
+    public int getOffsetPage() {
+        return offsetPage;
+    }
+
+    public NgramsSortFieldName getOrderByFieldName() {
+        return orderByFieldName;
+    }
+
+    public Order getOrder() {
+        return order;
+    }
+}

libngram-database/src/main/java/dao/daoQueryObjects/WordsListDaoQueryObject.java

+package dao.daoQueryObjects;
+
+import dao.queryOptions.Order;
+import dao.queryOptions.WordsSortFieldName;
+
+public class WordsListDaoQueryObject {
+    // required
+    private final int ngramId;
+    private final int offsetPage;
+    private final int amountOfItems;
+    private final String partOfWord;
+
+    // optional
+    private final WordsSortFieldName orderByFieldName;
+    private final Order order;
+
+    public WordsListDaoQueryObject(int ngramId, int pageOffset, int amountOfItems, String partOfWord, Enum orderByFieldName, Enum order) {
+        this.ngramId = ngramId;
+        this.offsetPage = pageOffset;
+        this.amountOfItems = amountOfItems;
+        this.partOfWord = partOfWord;
+
+        if (orderByFieldName == null)
+            this.orderByFieldName = null;
+        else
+            this.orderByFieldName = WordsSortFieldName.valueOf(orderByFieldName.name());
+
+        if (order == null)
+            this.order = null;
+        else
+            this.order = Order.valueOf(order.name());
+    }
+
+    public int getNgramId() {
+        return ngramId;
+    }
+
+    public int getOffsetPage() {
+        return offsetPage;
+    }
+
+    public int getAmountOfItems() {
+        return amountOfItems;
+    }
+
+    public String getPartOfWord() {
+        return partOfWord;
+    }
+
+    public WordsSortFieldName getOrderByFieldName() {
+        return orderByFieldName;
+    }
+
+    public Order getOrder() {
+        return order;
+    }
+}

libngram-database/src/main/java/dao/jdbcDao/JdbcNgramsDao.java

 
 import dao.AbstractDao;
 import dao.NgramsDao;
+import dao.daoQueryObjects.NgramsListDaoQueryObject;
 import dao.entities.dbTables.Ngram;
 import dao.exceptions.IllegalInputSizeException;
-import extractedDataFormats.WordWithCountOfNgram;
+import dao.queryOptions.NgramsSortFieldName;
+import dao.queryOptions.Order;
 
 import java.sql.Connection;
 import java.sql.ResultSet;
 
     private final static String SELECT_TEN_NGRAMS_QUERY_DATA_FIELD_NAME_PART = " n_gram_data ";
     private final static String SELECT_TEN_NGRAMS_QUERY_COUNTER_FIELD_NAME_PART = " n_gram_count ";
-    //------------------------------------------------------------------------------------------------------------------
-    private final static String SELECT_TEN_WORDS_FOR_NGRAM_WITH_COUNTERS_GENERAL_PART =
-            "SELECT WORD_DATA, NGRAM_IN_WORD_COUNT FROM WORDS AS A INNER JOIN N_GRAMS_TO_WORDS AS B ON A.ID = B.WORD_ID WHERE NGRAM_ID = ? ";
 
-    private final static String SELECT_TEN_WORDS_QUERY_DATA_FIELD_NAME_PART = " WORD_DATA ";
-    private final static String SELECT_TEN_WORDS_QUERY_COUNTER_FIELD_PART = " NGRAM_IN_WORD_COUNT ";
-    //*** General for both queries *************************************************************************************
     private final static String QUERY_ORDER_BY_CLAUSE = " ORDER BY ";
     private final static String QUERY_LIMITATION_PART = " LIMIT ? OFFSET ? * 10;";
-
     //******************************************************************************************************************
     public JdbcNgramsDao(Connection sourceConnection) {
         super(sourceConnection);
         return ngram.getId();
     }
 
-    private List<Ngram> extractListOfTenItems(int amountOfItems, int offsetPage, String query) {
+    private List<Ngram> extractListOfTenItems(String query, int amountOfItems, int offsetPage) {
         if (offsetPage < 0 || amountOfItems < 1)
             return null;
         try {
     }
 
     @Override
-    public List<Ngram> extractListOfItemsById(int offsetPage, int amountOfItems) {
-        return extractListOfTenItems(amountOfItems, offsetPage, SELECT_TEN_NGRAMS_GENERAL_PART + QUERY_LIMITATION_PART);
-    }
-
-    @Override
-    public List<Ngram> extractListOfItemsAscendByName(int offsetPage, int amountOfItems) {
-        return extractListOfTenItems(amountOfItems, offsetPage, SELECT_TEN_NGRAMS_GENERAL_PART + QUERY_ORDER_BY_CLAUSE +
-                SELECT_TEN_NGRAMS_QUERY_DATA_FIELD_NAME_PART + "ASC" + QUERY_LIMITATION_PART);
-    }
-
-    @Override
-    public List<Ngram> extractListOfItemsDescendByName(int offsetPage, int amountOfItems) {
-        return extractListOfTenItems(amountOfItems, offsetPage, SELECT_TEN_NGRAMS_GENERAL_PART + QUERY_ORDER_BY_CLAUSE +
-                SELECT_TEN_NGRAMS_QUERY_DATA_FIELD_NAME_PART + "DESC" + QUERY_LIMITATION_PART);
-    }
-
-    @Override
-    public List<Ngram> extractListOfItemsAscendByCount(int offsetPage, int amountOfItems) {
-        return extractListOfTenItems(amountOfItems, offsetPage, SELECT_TEN_NGRAMS_GENERAL_PART + QUERY_ORDER_BY_CLAUSE +
-                SELECT_TEN_NGRAMS_QUERY_COUNTER_FIELD_NAME_PART + "ASC" + QUERY_LIMITATION_PART);
-    }
-
-    @Override
-    public List<Ngram> extractListOfItemsDescendByCount(int offsetPage, int amountOfItems) {
-        return extractListOfTenItems(amountOfItems, offsetPage, SELECT_TEN_NGRAMS_GENERAL_PART + QUERY_ORDER_BY_CLAUSE +
-                SELECT_TEN_NGRAMS_QUERY_COUNTER_FIELD_NAME_PART + "DESC" + QUERY_LIMITATION_PART);
-    }
-
-    private List<WordWithCountOfNgram> extractListOfTenWordsWithCounters(String query, int ngramId, int amountOfItems, int offsetPage) {
-        if (offsetPage < 0)
+    public List<Ngram> extractListOfNgrams(NgramsListDaoQueryObject ngramsListDaoQueryObject) {
+        if (ngramsListDaoQueryObject == null) {
             return null;
-        try {
-            return querySelect(query, new Object[]{ngramId, amountOfItems, offsetPage},
-                    new Processor<List<WordWithCountOfNgram>>() {
-                        @Override
-                        public List<WordWithCountOfNgram> extractData(ResultSet rs) throws SQLException, IllegalInputSizeException {
-                            List<WordWithCountOfNgram> result = new ArrayList<WordWithCountOfNgram>();
-
-                            while (rs.next()) {
-                                result.add(new WordWithCountOfNgram(
-                                        rs.getString("WORD_DATA"),
-                                        rs.getInt("NGRAM_IN_WORD_COUNT")
-                                ));
-                            }
-                            if (result.size() == 0) {
-                                return null;
-                            }
-                            return result;
-                        }
-                    }
-            );
-        } catch (SQLException e) {
-            throw new RuntimeException(e);
-        } catch (IllegalInputSizeException e) {
-            throw new RuntimeException(e);
         }
-    }
-
-    @Override
-    public List<WordWithCountOfNgram> extractListOfWordsWithCountForNgramById(int ngramId, int offsetPage, int amountOfItems) {
-        return extractListOfTenWordsWithCounters(SELECT_TEN_WORDS_FOR_NGRAM_WITH_COUNTERS_GENERAL_PART + QUERY_LIMITATION_PART, ngramId, amountOfItems, offsetPage);
-    }
 
-    @Override
-    public List<WordWithCountOfNgram> extractListOfWordsWithCountForNgramAscendByWordData(int ngramId, int offsetPage, int amountOfItems) {
-        return extractListOfTenWordsWithCounters(SELECT_TEN_WORDS_FOR_NGRAM_WITH_COUNTERS_GENERAL_PART +
-                QUERY_ORDER_BY_CLAUSE + SELECT_TEN_WORDS_QUERY_DATA_FIELD_NAME_PART + "ASC" + QUERY_LIMITATION_PART, ngramId, amountOfItems, offsetPage);
-    }
+        String querySB;
 
-    @Override
-    public List<WordWithCountOfNgram> extractListOfWordsWithCountForNgramDescendByWordData(int ngramId, int offsetPage, int amountOfItems) {
-        return extractListOfTenWordsWithCounters(SELECT_TEN_WORDS_FOR_NGRAM_WITH_COUNTERS_GENERAL_PART +
-                QUERY_ORDER_BY_CLAUSE + SELECT_TEN_WORDS_QUERY_DATA_FIELD_NAME_PART + "DESC" + QUERY_LIMITATION_PART, ngramId, amountOfItems, offsetPage);
-    }
+        int amountOfItems = ngramsListDaoQueryObject.getAmount();
+        int offsetPage = ngramsListDaoQueryObject.getOffsetPage();
 
-    @Override
-    public List<WordWithCountOfNgram> extractListOfWordsWithCountForNgramAscendByCount(int ngramId, int offsetPage, int amountOfItems) {
-        return extractListOfTenWordsWithCounters(SELECT_TEN_WORDS_FOR_NGRAM_WITH_COUNTERS_GENERAL_PART +
-                QUERY_ORDER_BY_CLAUSE + SELECT_TEN_WORDS_QUERY_COUNTER_FIELD_PART + "ASC" + QUERY_LIMITATION_PART, ngramId, amountOfItems, offsetPage);
-    }
+        if (ngramsListDaoQueryObject.getOrder() == null && ngramsListDaoQueryObject.getOrderByFieldName() == null) {
+            querySB = SELECT_TEN_NGRAMS_GENERAL_PART + QUERY_LIMITATION_PART;
+            return extractListOfTenItems(querySB, amountOfItems, offsetPage);
+        }
 
-    @Override
-    public List<WordWithCountOfNgram> extractListOfWordsWithCountForNgramDescendByCount(int ngramId, int offsetPage, int amountOfItems) {
-        return extractListOfTenWordsWithCounters(SELECT_TEN_WORDS_FOR_NGRAM_WITH_COUNTERS_GENERAL_PART +
-                QUERY_ORDER_BY_CLAUSE + SELECT_TEN_WORDS_QUERY_COUNTER_FIELD_PART + "DESC" + QUERY_LIMITATION_PART, ngramId, amountOfItems, offsetPage);
+        if (ngramsListDaoQueryObject.getOrder() == Order.asc) {
+            if (ngramsListDaoQueryObject.getOrderByFieldName() == NgramsSortFieldName.name) { // ASC by name
+                querySB = SELECT_TEN_NGRAMS_GENERAL_PART + QUERY_ORDER_BY_CLAUSE + SELECT_TEN_NGRAMS_QUERY_DATA_FIELD_NAME_PART + "ASC" + QUERY_LIMITATION_PART;
+                return extractListOfTenItems(querySB, amountOfItems, offsetPage);
+            } else { // ASC by count
+                querySB = SELECT_TEN_NGRAMS_GENERAL_PART + QUERY_ORDER_BY_CLAUSE + SELECT_TEN_NGRAMS_QUERY_COUNTER_FIELD_NAME_PART + "ASC" + QUERY_LIMITATION_PART;
+                return extractListOfTenItems(querySB, amountOfItems, offsetPage);
+            }
+        } else {
+            if (ngramsListDaoQueryObject.getOrderByFieldName() == NgramsSortFieldName.name) { // DESC by name
+                querySB = SELECT_TEN_NGRAMS_GENERAL_PART + QUERY_ORDER_BY_CLAUSE + SELECT_TEN_NGRAMS_QUERY_DATA_FIELD_NAME_PART + "DESC" + QUERY_LIMITATION_PART;
+                return extractListOfTenItems(querySB, amountOfItems, offsetPage);
+            } else { // DESC by count
+                querySB = SELECT_TEN_NGRAMS_GENERAL_PART + QUERY_ORDER_BY_CLAUSE + SELECT_TEN_NGRAMS_QUERY_COUNTER_FIELD_NAME_PART + "DESC" + QUERY_LIMITATION_PART;
+                return extractListOfTenItems(querySB, amountOfItems, offsetPage);
+            }
+        }
     }
 
     @Override

libngram-database/src/main/java/dao/jdbcDao/JdbcWordsDao.java

 
 import dao.AbstractDao;
 import dao.WordsDao;
+import dao.daoQueryObjects.WordsListDaoQueryObject;
 import dao.entities.logical.WordForNgramWithCount;
 import dao.exceptions.IllegalInputSizeException;
+import dao.queryOptions.Order;
+import dao.queryOptions.WordsSortFieldName;
 
 import java.sql.Connection;
 import java.sql.ResultSet;
         }
     }
 
-    private List<WordForNgramWithCount> extractListOfTenItems(String query, int ngramId, String partOfWord, int amountOfItems, int pageOffset) {
-        if (partOfWord == null || ngramId < 0 || pageOffset < 0)
+    private List<WordForNgramWithCount> extractListOfTenItems(String query, int ngramId, String partOfWord, int amountOfItems, int offsetPage) {
+        if (partOfWord == null || ngramId < 0 || offsetPage < 0)
             return null;
         try {
-            return querySelect(query, new Object[]{ngramId, partOfWord, amountOfItems, pageOffset},
+            return querySelect(query, new Object[]{ngramId, partOfWord, amountOfItems, offsetPage},
                     new Processor<List<WordForNgramWithCount>>() {
                         @Override
                         public List<WordForNgramWithCount> extractData(ResultSet rs) throws SQLException, IllegalInputSizeException {
     }
 
     @Override
-    public List<WordForNgramWithCount> findAndExtractListOfWordsForNgramByItsDataPartialMatchSortById(String partOfWord, int ngramId, int amountOfItems, int pageOffset) {
-        String querySB = SELECT_TEN_WORDS_WITH_COUNTERS_FOR_NGRAM_BY_PARTIAL_WORD_DATA_GENERAL_PART + QUERY_LIMITATION_PART;
-        return extractListOfTenItems(querySB, ngramId, partOfWord, amountOfItems, pageOffset);
-    }
-
-    @Override
-    public List<WordForNgramWithCount> findAndExtractListOfWordsForNgramByItsDataPartialMatchSortByNameAsc(String partOfWord, int ngramId, int amountOfItems, int pageOffset) {
-        String querySB = SELECT_TEN_WORDS_WITH_COUNTERS_FOR_NGRAM_BY_PARTIAL_WORD_DATA_GENERAL_PART +
-                QUERY_ORDER_BY_CLAUSE + SELECT_TEN_WORDS_QUERY_DATA_FIELD_NAME_PART + "ASC" + QUERY_LIMITATION_PART;
-        return extractListOfTenItems(querySB, ngramId, partOfWord, amountOfItems, pageOffset);
-    }
+    public List<WordForNgramWithCount> findAndExtractListOfWordsForNgramByPartialWordMatch(WordsListDaoQueryObject wordsListDaoQueryObject) {
+        if (wordsListDaoQueryObject == null) {
+            return null;
+        }
+        String querySB;
 
-    @Override
-    public List<WordForNgramWithCount> findAndExtractListOfWordsForNgramByItsDataPartialMatchSortByNameDesc(String partOfWord, int ngramId, int amountOfItems, int pageOffset) {
-        String querySB = SELECT_TEN_WORDS_WITH_COUNTERS_FOR_NGRAM_BY_PARTIAL_WORD_DATA_GENERAL_PART +
-                QUERY_ORDER_BY_CLAUSE + SELECT_TEN_WORDS_QUERY_DATA_FIELD_NAME_PART + "DESC" + QUERY_LIMITATION_PART;
-        return extractListOfTenItems(querySB, ngramId, partOfWord, amountOfItems, pageOffset);
-    }
+        int ngramId = wordsListDaoQueryObject.getNgramId();
+        String partOfWord = wordsListDaoQueryObject.getPartOfWord();
+        int amountOfItems = wordsListDaoQueryObject.getAmountOfItems();
+        int offsetPage = wordsListDaoQueryObject.getOffsetPage();
 
-    @Override
-    public List<WordForNgramWithCount> findAndExtractListOfWordsForNgramByItsDataPartialMatchSortByCountAsc(String partOfWord, int ngramId, int amountOfItems, int pageOffset) {
-        String querySB = SELECT_TEN_WORDS_WITH_COUNTERS_FOR_NGRAM_BY_PARTIAL_WORD_DATA_GENERAL_PART +
-                QUERY_ORDER_BY_CLAUSE + SELECT_TEN_WORDS_QUERY_COUNTER_FIELD_PART + "ASC" + QUERY_LIMITATION_PART;
-        return extractListOfTenItems(querySB, ngramId, partOfWord, amountOfItems, pageOffset);
-    }
+        if (wordsListDaoQueryObject.getOrder() == null && wordsListDaoQueryObject.getOrderByFieldName() == null) {
+            querySB = SELECT_TEN_WORDS_WITH_COUNTERS_FOR_NGRAM_BY_PARTIAL_WORD_DATA_GENERAL_PART + QUERY_LIMITATION_PART;
+            return extractListOfTenItems(querySB, ngramId, partOfWord, amountOfItems, offsetPage);
+        }
 
-    @Override
-    public List<WordForNgramWithCount> findAndExtractListOfWordsForNgramByItsDataPartialMatchSortByCountDesc(String partOfWord, int ngramId, int amountOfItems, int pageOffset) {
-        String querySB = SELECT_TEN_WORDS_WITH_COUNTERS_FOR_NGRAM_BY_PARTIAL_WORD_DATA_GENERAL_PART +
-                QUERY_ORDER_BY_CLAUSE + SELECT_TEN_WORDS_QUERY_COUNTER_FIELD_PART + "DESC" + QUERY_LIMITATION_PART;
-        return extractListOfTenItems(querySB, ngramId, partOfWord, amountOfItems, pageOffset);
+        if (wordsListDaoQueryObject.getOrder() == Order.asc) {
+            if (wordsListDaoQueryObject.getOrderByFieldName() == WordsSortFieldName.word) { // ASC by word
+                querySB = SELECT_TEN_WORDS_WITH_COUNTERS_FOR_NGRAM_BY_PARTIAL_WORD_DATA_GENERAL_PART +
+                        QUERY_ORDER_BY_CLAUSE + SELECT_TEN_WORDS_QUERY_DATA_FIELD_NAME_PART + "ASC" + QUERY_LIMITATION_PART;
+                return extractListOfTenItems(querySB, ngramId, partOfWord, amountOfItems, offsetPage);
+            } else { // ASC by count
+                querySB = SELECT_TEN_WORDS_WITH_COUNTERS_FOR_NGRAM_BY_PARTIAL_WORD_DATA_GENERAL_PART +
+                        QUERY_ORDER_BY_CLAUSE + SELECT_TEN_WORDS_QUERY_COUNTER_FIELD_PART + "ASC" + QUERY_LIMITATION_PART;
+                return extractListOfTenItems(querySB, ngramId, partOfWord, amountOfItems, offsetPage);
+            }
+        } else {
+            if (wordsListDaoQueryObject.getOrderByFieldName() == WordsSortFieldName.word) { // DESC by word
+                querySB = SELECT_TEN_WORDS_WITH_COUNTERS_FOR_NGRAM_BY_PARTIAL_WORD_DATA_GENERAL_PART +
+                        QUERY_ORDER_BY_CLAUSE + SELECT_TEN_WORDS_QUERY_DATA_FIELD_NAME_PART + "DESC" + QUERY_LIMITATION_PART;
+                return extractListOfTenItems(querySB, ngramId, partOfWord, amountOfItems, offsetPage);
+            } else { // DESC by count
+                querySB = SELECT_TEN_WORDS_WITH_COUNTERS_FOR_NGRAM_BY_PARTIAL_WORD_DATA_GENERAL_PART +
+                        QUERY_ORDER_BY_CLAUSE + SELECT_TEN_WORDS_QUERY_COUNTER_FIELD_PART + "DESC" + QUERY_LIMITATION_PART;
+                return extractListOfTenItems(querySB, ngramId, partOfWord, amountOfItems, offsetPage);
+            }
+        }
     }
 
     @Override

libngram-database/src/main/java/dao/queryOptions/NgramsReaderFieldName.java

-package dao.queryOptions;
-
-public enum NgramsReaderFieldName {
-    name, count
-}

libngram-database/src/main/java/dao/queryOptions/NgramsSortFieldName.java

+package dao.queryOptions;
+
+public enum NgramsSortFieldName {
+    name, count
+}

libngram-database/src/main/java/extractedDataFormats/WordWithCountOfNgram.java

-package extractedDataFormats;
-
-public class WordWithCountOfNgram {
-    private final String word;
-    private final int count;
-
-    public WordWithCountOfNgram(String word, int count) {
-        this.word = word;
-        this.count = count;
-    }
-
-    public String getWord() {
-        return word;
-    }
-
-    public int getCount() {
-        return count;
-    }
-}

ngram-stats-web-view/src/main/java/servlets/ngram/ViewNgram.java

 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import queryObjects.WordsListQueryObject;
 import services.WordsDatabaseReaderInterface;
 import services.WordsJdbcReader;
 import servlets.rest.ViewNgramRest;
         }
 
         try {
-            if (orderFieldName != null) {
-                if (order != null) {
-                    listOfWordsForNgram = dbreader.getListOfWordsWithCounters(pageId - 1, AMOUNT_OF_ELEMENTS_PER_PAGE, ngramData, patternToFilter, orderFieldName, order);
-                    pathPostConditions = orderFieldName + "/" + order;
-                } else {
-                    listOfWordsForNgram = dbreader.getListOfWordsWithCounters(pageId - 1, AMOUNT_OF_ELEMENTS_PER_PAGE, ngramData, patternToFilter, orderFieldName);
-                    pathPostConditions = orderFieldName.name();
-                }
-            } else {
-                listOfWordsForNgram = dbreader.getListOfWordsWithCounters(pageId - 1, AMOUNT_OF_ELEMENTS_PER_PAGE, ngramData, patternToFilter);
-            }
+            listOfWordsForNgram = dbreader.getListOfWordsWithCounters(
+                    new WordsListQueryObject.WordsListQueryBuilder(ngramData, AMOUNT_OF_ELEMENTS_PER_PAGE, pageId - 1, patternToFilter)
+                            .orderByField(orderFieldName)
+                            .order(order).build()
+            );
+
         } catch (IllegalArgumentException e) {
-            listOfWordsForNgram = dbreader.getListOfWordsWithCounters(0, AMOUNT_OF_ELEMENTS_PER_PAGE, ngramData, REGEX_PATTERN_TO_EXTRACT_ALL_WORDS);
+            listOfWordsForNgram = dbreader.getListOfWordsWithCounters(
+                    new WordsListQueryObject.WordsListQueryBuilder(ngramData, AMOUNT_OF_ELEMENTS_PER_PAGE, 0, patternToFilter)
+                            .orderByField(orderFieldName)
+                            .order(order).build()
+            );
         }
 
         final int finalPageId = pageId;

ngram-stats-web-view/src/main/java/servlets/ngrams/ViewNgrams.java

 package servlets.ngrams;
 
 import dao.entities.dbTables.Ngram;
-import dao.queryOptions.NgramsReaderFieldName;
+import dao.queryOptions.NgramsSortFieldName;
 import dao.queryOptions.Order;
 import exceptions.WrongURIException;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import queryObjects.NgramsListQueryObject;
 import services.NgramsDatabaseReaderInterface;
 import services.NgramsJdbcReader;
 import servlets.rest.NgramsViewRest;
         final NgramsViewRest restParser = new NgramsViewRest(request.getRequestURI());
         pageId = restParser.getPageId();
 
-        NgramsReaderFieldName orderFieldName;
+        NgramsSortFieldName orderFieldName;
         Order order;
 
         try {
-            orderFieldName = NgramsReaderFieldName.valueOf(restParser.getOrderByFieldName());
+            orderFieldName = NgramsSortFieldName.valueOf(restParser.getOrderByFieldName());
         } catch (IllegalArgumentException e) {
             orderFieldName = null;
         } catch (Exception e) {
         }
 
         try {
-            if (orderFieldName != null) {
-                if (order != null) {
-                    listOfNgrams = dbreader.getListOfNgrams(pageId - 1, AMOUNT_OF_ELEMENTS_PER_PAGE, orderFieldName, order);
-                    pathConditionsPart = orderFieldName + "/" + order;
-                } else {
-                    listOfNgrams = dbreader.getListOfNgrams(pageId - 1, AMOUNT_OF_ELEMENTS_PER_PAGE, orderFieldName);
-                    pathConditionsPart = orderFieldName.name();
-                }
-            } else {
-                listOfNgrams = dbreader.getListOfNgrams(pageId - 1, AMOUNT_OF_ELEMENTS_PER_PAGE);
-            }
+            listOfNgrams = dbreader.getListOfNgrams(
+                    new NgramsListQueryObject.NgramsListQueryBuilder(AMOUNT_OF_ELEMENTS_PER_PAGE, pageId - 1)
+                            .orderByField(orderFieldName)
+                            .order(order).build()
+            );
         } catch (IllegalArgumentException e) {
-            listOfNgrams = dbreader.getListOfNgrams(0, AMOUNT_OF_ELEMENTS_PER_PAGE);
+            listOfNgrams = dbreader.getListOfNgrams(
+                    new NgramsListQueryObject.NgramsListQueryBuilder(AMOUNT_OF_ELEMENTS_PER_PAGE, pageId - 1)
+                            .orderByField(orderFieldName)
+                            .order(order).build()
+            );
         }
 
         final int finalPageId = pageId;