src/net/java/otr4j/OtrPolicyImpl.java
changeset 928 0ff0059f2ec3
equal deleted inserted replaced
797:fbd3585af53e 928:0ff0059f2ec3
       
     1 package net.java.otr4j;
       
     2 
       
     3 public class OtrPolicyImpl implements OtrPolicy {
       
     4 
       
     5 	public OtrPolicyImpl() {
       
     6 		this.setPolicy(NEVER);
       
     7 	}
       
     8 
       
     9 	public OtrPolicyImpl(int policy) {
       
    10 		this.setPolicy(policy);
       
    11 	}
       
    12 
       
    13 	private int policy;
       
    14 
       
    15 	public int getPolicy() {
       
    16 		return policy;
       
    17 	}
       
    18 
       
    19 	private void setPolicy(int policy) {
       
    20 		this.policy = policy;
       
    21 	}
       
    22 
       
    23 	public boolean getAllowV1() {
       
    24 		return (policy & OtrPolicy.ALLOW_V1) != 0;
       
    25 	}
       
    26 
       
    27 	public boolean getAllowV2() {
       
    28 		return (policy & OtrPolicy.ALLOW_V2) != 0;
       
    29 	}
       
    30 
       
    31 	public boolean getErrorStartAKE() {
       
    32 		return (policy & OtrPolicy.ERROR_START_AKE) != 0;
       
    33 	}
       
    34 
       
    35 	public boolean getRequireEncryption() {
       
    36 		return getEnableManual()
       
    37 				&& (policy & OtrPolicy.REQUIRE_ENCRYPTION) != 0;
       
    38 	}
       
    39 
       
    40 	public boolean getSendWhitespaceTag() {
       
    41 		return (policy & OtrPolicy.SEND_WHITESPACE_TAG) != 0;
       
    42 	}
       
    43 
       
    44 	public boolean getWhitespaceStartAKE() {
       
    45 		return (policy & OtrPolicy.WHITESPACE_START_AKE) != 0;
       
    46 	}
       
    47 
       
    48 	public void setAllowV1(boolean value) {
       
    49 		if (value)
       
    50 			policy |= ALLOW_V1;
       
    51 		else
       
    52 			policy &= ~ALLOW_V1;
       
    53 	}
       
    54 
       
    55 	public void setAllowV2(boolean value) {
       
    56 		if (value)
       
    57 			policy |= ALLOW_V2;
       
    58 		else
       
    59 			policy &= ~ALLOW_V2;
       
    60 	}
       
    61 
       
    62 	public void setErrorStartAKE(boolean value) {
       
    63 		if (value)
       
    64 			policy |= ERROR_START_AKE;
       
    65 		else
       
    66 			policy &= ~ERROR_START_AKE;
       
    67 	}
       
    68 
       
    69 	public void setRequireEncryption(boolean value) {
       
    70 		if (value)
       
    71 			policy |= REQUIRE_ENCRYPTION;
       
    72 		else
       
    73 			policy &= ~REQUIRE_ENCRYPTION;
       
    74 	}
       
    75 
       
    76 	public void setSendWhitespaceTag(boolean value) {
       
    77 		if (value)
       
    78 			policy |= SEND_WHITESPACE_TAG;
       
    79 		else
       
    80 			policy &= ~SEND_WHITESPACE_TAG;
       
    81 	}
       
    82 
       
    83 	public void setWhitespaceStartAKE(boolean value) {
       
    84 		if (value)
       
    85 			policy |= WHITESPACE_START_AKE;
       
    86 		else
       
    87 			policy &= ~WHITESPACE_START_AKE;
       
    88 	}
       
    89 
       
    90 	public boolean getEnableAlways() {
       
    91 		return getEnableManual() && getErrorStartAKE()
       
    92 				&& getSendWhitespaceTag() && getWhitespaceStartAKE();
       
    93 	}
       
    94 
       
    95 	public void setEnableAlways(boolean value) {
       
    96 		if (value)
       
    97 			setEnableManual(true);
       
    98 
       
    99 		setErrorStartAKE(value);
       
   100 		setSendWhitespaceTag(value);
       
   101 		setWhitespaceStartAKE(value);
       
   102 
       
   103 	}
       
   104 
       
   105 	public boolean getEnableManual() {
       
   106 		return getAllowV1() && getAllowV2();
       
   107 	}
       
   108 
       
   109 	public void setEnableManual(boolean value) {
       
   110 		setAllowV1(value);
       
   111 		setAllowV2(value);
       
   112 	}
       
   113 
       
   114 	public boolean equals(Object obj) {
       
   115 		if (obj == this)
       
   116 			return true;
       
   117 		if (obj == null || obj.getClass() != this.getClass())
       
   118 			return false;
       
   119 
       
   120 		OtrPolicy policy = (OtrPolicy) obj;
       
   121 
       
   122 		return policy.getPolicy() == this.getPolicy();
       
   123 	}
       
   124 
       
   125 	public int hashCode() {
       
   126 		return this.getPolicy();
       
   127 	}
       
   128 }