1 /* 2 * Copyright 2009 Mike Cumings 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.kenai.jbosh; 18 19 /** 20 * Data type representing the getValue of the {@code ver} attribute of the 21 * {@code bosh} element. 22 */ 23 final class AttrVersion extends AbstractAttr<String> implements Comparable { 24 25 /** 26 * Default value if none is provided. 27 */ 28 private static final AttrVersion DEFAULT; 29 static { 30 try { 31 DEFAULT = createFromString("1.8"); 32 } catch (BOSHException boshx) { 33 throw(new IllegalStateException(boshx)); 34 } 35 } 36 37 /** 38 * Major portion of the version. 39 */ 40 private final int major; 41 42 /** 43 * Minor portion of the version. 44 */ 45 private final int minor; 46 47 /** 48 * Creates a new attribute object. 49 * 50 * @param val attribute getValue 51 * @throws BOSHException on parse or validation failure 52 */ 53 private AttrVersion(final String val) throws BOSHException { 54 super(val); 55 56 int idx = val.indexOf('.'); 57 if (idx <= 0) { 58 throw(new BOSHException( 59 "Illegal ver attribute value (not in major.minor form): " 60 + val)); 61 } 62 63 String majorStr = val.substring(0, idx); 64 try { 65 major = Integer.parseInt(majorStr); 66 } catch (NumberFormatException nfx) { 67 throw(new BOSHException( 68 "Could not parse ver attribute value (major ver): " 69 + majorStr, 70 nfx)); 71 } 72 if (major < 0) { 73 throw(new BOSHException( 74 "Major version may not be < 0")); 75 } 76 77 String minorStr = val.substring(idx + 1); 78 try { 79 minor = Integer.parseInt(minorStr); 80 } catch (NumberFormatException nfx) { 81 throw(new BOSHException( 82 "Could not parse ver attribute value (minor ver): " 83 + minorStr, 84 nfx)); 85 } 86 if (minor < 0) { 87 throw(new BOSHException( 88 "Minor version may not be < 0")); 89 } 90 } 91 92 /** 93 * Get the version of specifcation that we support. 94 * 95 * @return max spec version the code supports 96 */ 97 static AttrVersion getSupportedVersion() { 98 return DEFAULT; 99 } 100 101 /** 102 * Creates a new attribute instance from the provided String. 103 * 104 * @param str string representation of the attribute 105 * @return attribute instance or {@code null} if provided string is 106 * {@code null} 107 * @throws BOSHException on parse or validation failure 108 */ 109 static AttrVersion createFromString(final String str) 110 throws BOSHException { 111 if (str == null) { 112 return null; 113 } else { 114 return new AttrVersion(str); 115 } 116 } 117 118 /** 119 * Returns the 'major' portion of the version number. 120 * 121 * @return major digits only 122 */ 123 int getMajor() { 124 return major; 125 } 126 127 /** 128 * Returns the 'minor' portion of the version number. 129 * 130 * @return minor digits only 131 */ 132 int getMinor() { 133 return minor; 134 } 135 136 /////////////////////////////////////////////////////////////////////////// 137 // Comparable interface: 138 139 /** 140 * {@inheritDoc} 141 * 142 * @param otherObj object to compare to 143 * @return -1, 0, or 1 144 */ 145 @Override 146 public int compareTo(final Object otherObj) { 147 if (otherObj instanceof AttrVersion) { 148 AttrVersion other = (AttrVersion) otherObj; 149 if (major < other.major) { 150 return -1; 151 } else if (major > other.major) { 152 return 1; 153 } else if (minor < other.minor) { 154 return -1; 155 } else if (minor > other.minor) { 156 return 1; 157 } else { 158 return 0; 159 } 160 } else { 161 return 0; 162 } 163 } 164 165 } 166