1
\$\begingroup\$

Following is code which takes three arguments as

  1. String
  2. Map
  3. String

now I already have some predefined methods which return the requirement of both String as List and since my map contains only two keys (for now) and all I get is their output in form of List which is then converted to set and added to set.

Now what I want to achieve is to reduce the number of declarations as they all merge in the same set.Now the question is there any way to reduce the noisy declarations of set or I should keep it the way these are

 @Override
        public Set<Product> getProductByCategoryPriceManfacturer(String category, Map<String, String> priceParms,
                String manufacturer) {
            Set<String> priceFilter = priceParms.keySet();
            Set<Product> lowPrice = new TreeSet<>();
            Set<Product> highPrice = new TreeSet<>();
            Set<Product> productByCategory = new TreeSet<>(this.getProductByCategory(category));
            Set<Product> productByManfacturer = new TreeSet<>(this.getProductsByManufacturer(manufacturer));
            productByCategory.addAll(productByManfacturer);

            if (priceFilter.contains("low")) {
                for (String low : priceParms.get("low")) {
                    lowPrice.addAll(this.getProductByMinimumPrice(low));
                }
            }
            if (priceFilter.contains("high")) {
                for (String high : priceParms.get("high")) {
                    highPrice.addAll(this.getProductByMaximumPrice(high));
                }
            }
            productByCategory.addAll(highPrice);
            productByCategory.addAll(lowPrice);
            return productByCategory;
        }

Note where all arguments in constructors of Set are List.

Now, is there any way to reduce the code I mean if I can reduce the number of declarations.

Edit In order to reduce the noise I ended up using Map<String,String> and now the method is as follow, can It be reduced more?

@Override
    public Set<Product> getProductByCategoryPriceManfacturer(String category, Map<String, String> priceParms,
            String manufacturer) {
        BigDecimal minPrice = new BigDecimal(priceParms.get("low".trim()));
        BigDecimal maxPrice = new BigDecimal(priceParms.get("high".trim()));

        return listAllProducts.stream().filter(product -> product.getCategory().equalsIgnoreCase(category))
                .filter(productMinPrice -> productMinPrice.getUnitPrice().compareTo(maxPrice) < 0)
                .filter(minPriceProduct -> minPriceProduct.getUnitPrice().compareTo(minPrice) > 0)
                .collect(Collectors.toSet());

    }

Edit I have one more question as this is repository class in springMVC the only one liner suggestion i want that, is that good to perform all operations of databases in Repository class given the fact that running query for every single call will make it slow to load products or i just load all products at once and perform all operations after that?

\$\endgroup\$
  • \$\begingroup\$ Do you have the possibility to use Java 8 ? \$\endgroup\$ – Simon Forsberg Sep 14 '16 at 11:25
  • \$\begingroup\$ actually java8 will make this one liner which will be great :) \$\endgroup\$ – emotionlessbananas Sep 14 '16 at 11:34
  • \$\begingroup\$ I'm not so sure it will all be a one-liner but it will simplify some things. Also, how is the implementation for getProductByMaximumPrice and getProductByMinimumPrice ? \$\endgroup\$ – Simon Forsberg Sep 14 '16 at 12:36
  • \$\begingroup\$ @SimonForberg i edited/modified the question with a sample result, please check \$\endgroup\$ – emotionlessbananas Sep 15 '16 at 7:05
  • \$\begingroup\$ Calling "high".trim() has no effect, did you mean priceParms.get("high").trim() ? \$\endgroup\$ – Simon Forsberg Sep 15 '16 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.