View Javadoc
1   package org.kuali.common.httplib.api.model;
2   
3   import static java.lang.String.format;
4   import static org.kuali.common.jute.base.Precondition.checkMin;
5   import static org.kuali.common.jute.base.Precondition.checkNotBlank;
6   
7   import org.apache.http.ProtocolVersion;
8   
9   import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
10  
11  @JsonDeserialize(builder = Protocol.Builder.class)
12  public final class Protocol {
13  
14      private final String name;
15      private final int majorVersion;
16      private final int minorVersion;
17  
18      private Protocol(Builder builder) {
19          this.name = builder.name;
20          this.majorVersion = builder.majorVersion;
21          this.minorVersion = builder.minorVersion;
22      }
23  
24      public static Protocol build(ProtocolVersion mutable) {
25          Builder builder = builder();
26          builder.withName(mutable.getProtocol());
27          builder.withMajorVersion(mutable.getMajor());
28          builder.withMinorVersion(mutable.getMinor());
29          return builder.build();
30      }
31  
32      public static Builder builder() {
33          return new Builder();
34      }
35  
36      public static class Builder implements org.apache.commons.lang3.builder.Builder<Protocol> {
37  
38          private String name;
39          private int majorVersion;
40          private int minorVersion;
41  
42          public Builder withName(String name) {
43              this.name = name;
44              return this;
45          }
46  
47          public Builder withMajorVersion(int majorVersion) {
48              this.majorVersion = majorVersion;
49              return this;
50          }
51  
52          public Builder withMinorVersion(int minorVersion) {
53              this.minorVersion = minorVersion;
54              return this;
55          }
56  
57          @Override
58          public Protocol build() {
59              return validate(new Protocol(this));
60          }
61  
62          private static Protocol validate(Protocol instance) {
63              checkNotBlank(instance.name, "name");
64              checkMin(instance.majorVersion, 0, "majorVersion");
65              checkMin(instance.minorVersion, 0, "minorVersion");
66              return instance;
67          }
68  
69      }
70  
71      public String getName() {
72          return name;
73      }
74  
75      public int getMajorVersion() {
76          return majorVersion;
77      }
78  
79      public int getMinorVersion() {
80          return minorVersion;
81      }
82  
83      @Override
84      public String toString() {
85          return format("%s/%s.%s", name, majorVersion, minorVersion);
86      }
87  
88  }