How to check for invalid inputs in the Builder pattern class?

user2467545

Below is my builder class in which two fields are mandatory which are userId and clientId.

public final class InputKeys {

    private final long userId;
    private final int clientId;
    private final long timeout;
    private final Preference preferences;
    private final boolean debugFlag;
    private final Map<String, String> attributeMap;

    private InputKeys(Builder builder) {
    this.userId = builder.userId;
    this.clientId = builder.clientId;
    this.preferences = builder.preference;
    this.attributeMap = builder.attributeMap;
    this.timeout = builder.timeout;
    this.debugFlag = builder.debugFlag;
    }

    public static class Builder {
    protected final long userId;
    protected final int clientId;
    protected long timeout = 500L;
    protected Preference preference;
    protected boolean debugFlag;
    protected Map<String, String> attributeMap;


    public Builder(long userId, int clientId) {
        this.userId = userId;
        this.clientId = clientId;
    }

    public Builder attributeMap(Map<String, String> attributeMap) {
        this.attributeMap = attributeMap;
        return this;
    }

    public Builder preference(Preference preference) {
        this.preference = preference;
        return this;
    }

    public Builder debugFlag(boolean debugFlag) {
        this.debugFlag = debugFlag;
        return this;
    }

    public Builder timeout(long timeout) {
        this.timeout = timeout;
        return this;
    }

    public InputKeys build() {
        return new InputKeys(this);
    }
    }

    //getters  here
}

Now I will be calling this builder class like this -

InputKeys keys = new InputKeys.Builder(12000L, 33L).build();

But it might be possible that somebody can pass wrong inputs values such as they are passing negative userId's and negative clientId's, negative timeout values or empty attributeMap. How to deal in these kind of situation in my builder class?

If I am having IllegalArgumentcheck for each variables in if else if block, then my whole Builder class gets flooded up with IllegalArgumentException check?

Is there any better way of doing this?

Evgeniy Dorofeev

Make methods which have common logic, eg

private void assertNonNegative(long val, String attr) {
    if (val < 0) {
         throw IllegalArgumentException(attr + " cannot be negative");
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to check for invalid inputs in the Builder pattern class?

From Dev

How to implement Parcelable to a class with a Builder pattern

From Dev

Angularjs - How to check for unique inputs and if there is a duplicate mark both as invalid

From Dev

How To Always Check For Inputs

From Dev

What class is this Builder pattern extending?

From Dev

Builder pattern, templates and nested class

From Dev

Subclassing abstract class in a builder pattern?

From Dev

Builder Pattern with only one class

From Java

How to implement Builder pattern in Kotlin?

From Dev

Builder Pattern inside vs outside class?

From Dev

Class with many parameters, beyond the Builder pattern

From Dev

Generic Builder Pattern class using reflection

From Dev

Java Builder Design Pattern Redundant Field Declaration in Class and its Builder

From Dev

How to better partition valid or invalid inputs

From Dev

How to mark invalid values/inputs with Editor, EditorDriver

From Dev

How to mark invalid values/inputs with Editor, EditorDriver

From Dev

How to better partition valid or invalid inputs

From Dev

How to differ inputs with same class?

From Dev

How to add a class to entityform inputs

From Dev

Should QAbstractItemModel::index(row, column, parent) check for invalid inputs?

From Dev

How to use Builder pattern as described by Joshua Bloch's version in my ModelInput class?

From Dev

How to check number inputs in the Linux shell?

From Dev

how to check for duplicate inputs and prevents it to be added to the database

From Dev

Builder pattern

From Dev

Builder pattern

From Dev

How to use builder pattern with all parameters as mandatory?

From Dev

How to build abstract classes using builder pattern?

From Dev

How to implement the builder pattern in Java 8?

From Dev

How to initialize a bean which follows the Builder Pattern

Related Related

  1. 1

    How to check for invalid inputs in the Builder pattern class?

  2. 2

    How to implement Parcelable to a class with a Builder pattern

  3. 3

    Angularjs - How to check for unique inputs and if there is a duplicate mark both as invalid

  4. 4

    How To Always Check For Inputs

  5. 5

    What class is this Builder pattern extending?

  6. 6

    Builder pattern, templates and nested class

  7. 7

    Subclassing abstract class in a builder pattern?

  8. 8

    Builder Pattern with only one class

  9. 9

    How to implement Builder pattern in Kotlin?

  10. 10

    Builder Pattern inside vs outside class?

  11. 11

    Class with many parameters, beyond the Builder pattern

  12. 12

    Generic Builder Pattern class using reflection

  13. 13

    Java Builder Design Pattern Redundant Field Declaration in Class and its Builder

  14. 14

    How to better partition valid or invalid inputs

  15. 15

    How to mark invalid values/inputs with Editor, EditorDriver

  16. 16

    How to mark invalid values/inputs with Editor, EditorDriver

  17. 17

    How to better partition valid or invalid inputs

  18. 18

    How to differ inputs with same class?

  19. 19

    How to add a class to entityform inputs

  20. 20

    Should QAbstractItemModel::index(row, column, parent) check for invalid inputs?

  21. 21

    How to use Builder pattern as described by Joshua Bloch's version in my ModelInput class?

  22. 22

    How to check number inputs in the Linux shell?

  23. 23

    how to check for duplicate inputs and prevents it to be added to the database

  24. 24

    Builder pattern

  25. 25

    Builder pattern

  26. 26

    How to use builder pattern with all parameters as mandatory?

  27. 27

    How to build abstract classes using builder pattern?

  28. 28

    How to implement the builder pattern in Java 8?

  29. 29

    How to initialize a bean which follows the Builder Pattern

HotTag

Archive