1
\$\begingroup\$

First let's introduce the problem:

I have a list like [{10,18}, {19,25}, {26,50}] and i have an input (n), when (n) inside one of objects in a list i will get this object and when (n) not higher then all objects in a list I want to get the higher object.

Take this list for example List<MyObject> list = [{10,18}, {19,25}, {26,50}];

when (n) =  5 ==> I will get {}
when (n) = 10 ==> I will get {10,18}
when (n) = 18 ==> I will get {10,18}
when (n) = 23 ==> I will get {19,25}
when (n) = 45 ==> I will get {26,50}
when (n) = 52 ==> I will get {26,50}

My code works fine, and I want a review.

I have a class Demo:

public class Demo {
    int age1;
    int age2;
    //getters and setters and constr
}

And a list of demos like

List<Demo> demos = new ArrayList<>();
demos.add(new Demo(11, 12));
demos.add(new Demo(13, 15));

I want to get demo object by value inside interval.

For example, int result = 13; I get first object.

When I have value higher then all the objects in the list, then I should get the object containing the highest value.

I have the logic in an interface with implementation.

public interface MyDemo {
    Function<Integer, Predicate<Demo>> checkBetweenInterval = value -> o -> value >= o.getAge1() && value <= o.getAge2();

    List<Demo> toListTraitement(List<Demo> demos, int number);

    default Optional<Demo> getDemo(List<Demo> demos, int number){
       return toListTraitement(demos, number).stream().filter(checkBetweenInterval.apply(number)).findFirst();
    }

    default Optional<Demo> getIfInLastObject(List<Demo> demos, int number){
        Demo last = demos.get(demos.size() - 1);
        if(number >= last.getAge2()) {
            return Optional.of(last);
        }
        else {
            return null;
        }
    }
     default Optional<Demo> getFinalResult(List<Demo> demos, int number) {
        Optional<Demo> demo = getDemo(demos, number);
        if(demo.isPresent()) {
            return demo;
        }
        else {
           return getIfInLastObject(demos, number);
        }
    }
}

Is this the right approach? Can I do better?

\$\endgroup\$
4
  • \$\begingroup\$ Welcome to Code Review! I think a few more examples would help understanding what you need to do. For example given the list [{11, 12}, {13, 15}] what should be the answer for inputs 5, 11, 12, 13, 15, 55? (Some of them I already understood, I'm saying if you add these examples into your question, it will be helpful.) \$\endgroup\$
    – janos
    Oct 28, 2021 at 17:50
  • \$\begingroup\$ Thank you @janos I edit my answer \$\endgroup\$
    – Akram
    Oct 28, 2021 at 18:21
  • \$\begingroup\$ I was most interested in what is returned when n is on interval boundaries, and when n is lower than all intervals. So in your example of [{10,18}, {19,25}, {26,50}], what will be the answer for 5, 10, 18 ? \$\endgroup\$
    – janos
    Oct 28, 2021 at 18:24
  • \$\begingroup\$ Ok, so when n = 5 I will get nothing and when n = 10 I will get {10,18} and when n = 18 I will get {10,18}, thank you a lot @janos \$\endgroup\$
    – Akram
    Oct 28, 2021 at 18:29

1 Answer 1

1
\$\begingroup\$

Firstly, if you're going to implement all the methods in an interface just make a class. A better way to structure this would be to have an IDemo with just the interface method definitions, then implement it with a MyDemo class. If you have multiple Demo instances that share the logic presently implemented, discard the interface and just use a Demo class.

Secondly, you're not using Optional to its fullest potential. You should never be returning null if Optional is an expected return type. Optional.empty() is the functional replacement. Honestly, for this application (which I'm assuming is in some kind of backend context) just work with truthy operations on null. Also, you can get those if-else blocks on one line.

Lastly, the method naming needs some work. I'm guessing English is not your first language (and congrats on speaking it better than most people who use it natively), but what is toListTraitement()? There are no comments defining the broader context, what a Demo even is, etc.

\$\endgroup\$
1
  • \$\begingroup\$ Thank @T145 for your answer, for structure i prefer to use default method feature for JAVA 8 that why I don't want to implement unnecessary methods. Sorry for the naming of methods and class that because I need just the idea to avoid added unnecessary code. \$\endgroup\$
    – Akram
    Oct 31, 2021 at 17:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.