0

I have an API endpoint where I am receiving JSON files, and I am creating an object from the files. I also have another pre-existing object, and I am trying to check if certain values in the received JSON match certain values in my existing object. If the fields match I will continue to process the file further, if not I will scrap it. My thoughts so far is just to have if statements checking each value, but is there a better way to do this? Or are if statements ok?

Very quick code example of what I mean by just using if statements.

public boolean compareObjects(recievedObject, existingObject) {
    if( !(recievedObject.getName().equals(existingObject.getName()))) {
        //true
    } else if( !(recievedObject.getLocation().equals(existingObject.getLocation())) ) {
        return false;
    }
    // else if ... etc 

    return true;
}

Note that I am not trying to check if the received file has all the required JSON fields, just that a few particular fields have certain values.

Edit:

The JSON will be a very flat structure e.g

{
    "name": "name",
    "location": "location",
    ...
}

So my object will be pretty basic

public class recievedObject {
    String location;
    String name;

    public String getLocation() {
        return location;
    }

    public String getName() {
        return name;
    }
}
shmosel
  • 49,289
  • 6
  • 73
  • 138
daveyjones
  • 123
  • 3
  • 10

2 Answers2

0

define a method similar to 'equal' for your class and at the endpoint check the existingObject.check(receivedObject), add import java.util.Objects to your class

public boolean check(Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }
    RecievedObject receivedObject=(RecievedObject) o;

   //add based on the logic you want
    return Objects.equals(location, receivedObject.location) &&
            Objects.equals(name, receivedObject.name);
}
khodayar J
  • 914
  • 8
  • 16
0

What you can do to avoid lot of it-else statements is to create some validation abstraction.

interface Validator<A, B> {
  boolean validate(A receivedObject, B existingObject);
}

Then for each if create new implementation of Validator.

class NameValidator implements Validator<Expeceted, Received> {
  @Override
  public boolean validate(Expeceted receivedObject, Received existingObject) {
    return existingObject.getName().equals(receivedObject.getName());
  }
}   

class LocationValidator implements Validator<Expeceted, Received> {
  @Override
  public boolean validate(Expeceted receivedObject, Received existingObject) {
    return existingObject.getLocation().equals(receivedObject.getLocation());
  }
}

You can create list of such a validators

List<Validator<Expeceted, Received>> validators = Arrays.asList(
  new NameValidator(),
  new LocationValidator()
);

And finally your compare method could simply iterate through all validators.

public boolean compareObjects(Received recievedObject, Expeceted expecetedObject) {
  for (Validator<Expeceted, Received> validation : validators) {
    if (! validation.validate(expecetedObject, recievedObject)) {
      return false;
    }
  }
  return true;
}

This way you can simply add new validators later and keep compare method untouched.

Ján Čabala
  • 184
  • 1
  • 7