選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Timeout.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (C) 2011, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.storage.dht;
  44. import java.text.MessageFormat;
  45. import java.util.concurrent.TimeUnit;
  46. import java.util.regex.Matcher;
  47. import java.util.regex.Pattern;
  48. import org.eclipse.jgit.lib.Config;
  49. import org.eclipse.jgit.util.StringUtils;
  50. /** Length of time to wait for an operation before giving up. */
  51. public class Timeout {
  52. /**
  53. * Construct a new timeout, expressed in milliseconds.
  54. *
  55. * @param millis
  56. * number of milliseconds to wait.
  57. * @return the timeout.
  58. */
  59. public static Timeout milliseconds(int millis) {
  60. return new Timeout(millis, TimeUnit.MILLISECONDS);
  61. }
  62. /**
  63. * Construct a new timeout, expressed in seconds.
  64. *
  65. * @param sec
  66. * number of seconds to wait.
  67. * @return the timeout.
  68. */
  69. public static Timeout seconds(int sec) {
  70. return new Timeout(sec, TimeUnit.SECONDS);
  71. }
  72. /**
  73. * Construct a new timeout, expressed in (possibly fractional) seconds.
  74. *
  75. * @param sec
  76. * number of seconds to wait.
  77. * @return the timeout.
  78. */
  79. public static Timeout seconds(double sec) {
  80. return new Timeout((long) (sec * 1000), TimeUnit.MILLISECONDS);
  81. }
  82. /**
  83. * Obtain a timeout from the configuration.
  84. *
  85. * @param cfg
  86. * configuration to read.
  87. * @param section
  88. * section key to read.
  89. * @param subsection
  90. * subsection to read, may be null.
  91. * @param name
  92. * variable to read.
  93. * @param defaultValue
  94. * default to return if no timeout is specified in the
  95. * configuration.
  96. * @return the configured timeout.
  97. */
  98. public static Timeout getTimeout(Config cfg, String section,
  99. String subsection, String name, Timeout defaultValue) {
  100. String valStr = cfg.getString(section, subsection, name);
  101. if (valStr == null)
  102. return defaultValue;
  103. valStr = valStr.trim();
  104. if (valStr.length() == 0)
  105. return defaultValue;
  106. Matcher m = matcher("^([1-9][0-9]*(?:\\.[0-9]*)?)\\s*(.*)$", valStr);
  107. if (!m.matches())
  108. throw notTimeUnit(section, subsection, name, valStr);
  109. String digits = m.group(1);
  110. String unitName = m.group(2).trim();
  111. long multiplier;
  112. TimeUnit unit;
  113. if ("".equals(unitName)) {
  114. multiplier = 1;
  115. unit = TimeUnit.MILLISECONDS;
  116. } else if (anyOf(unitName, "ms", "millisecond", "milliseconds")) {
  117. multiplier = 1;
  118. unit = TimeUnit.MILLISECONDS;
  119. } else if (anyOf(unitName, "s", "sec", "second", "seconds")) {
  120. multiplier = 1;
  121. unit = TimeUnit.SECONDS;
  122. } else if (anyOf(unitName, "m", "min", "minute", "minutes")) {
  123. multiplier = 60;
  124. unit = TimeUnit.SECONDS;
  125. } else if (anyOf(unitName, "h", "hr", "hour", "hours")) {
  126. multiplier = 3600;
  127. unit = TimeUnit.SECONDS;
  128. } else
  129. throw notTimeUnit(section, subsection, name, valStr);
  130. if (digits.indexOf('.') == -1) {
  131. try {
  132. return new Timeout(multiplier * Long.parseLong(digits), unit);
  133. } catch (NumberFormatException nfe) {
  134. throw notTimeUnit(section, subsection, name, valStr);
  135. }
  136. } else {
  137. double inputTime;
  138. try {
  139. inputTime = multiplier * Double.parseDouble(digits);
  140. } catch (NumberFormatException nfe) {
  141. throw notTimeUnit(section, subsection, name, valStr);
  142. }
  143. if (unit == TimeUnit.MILLISECONDS) {
  144. TimeUnit newUnit = TimeUnit.NANOSECONDS;
  145. long t = (long) (inputTime * newUnit.convert(1, unit));
  146. return new Timeout(t, newUnit);
  147. } else if (unit == TimeUnit.SECONDS && multiplier == 1) {
  148. TimeUnit newUnit = TimeUnit.MILLISECONDS;
  149. long t = (long) (inputTime * newUnit.convert(1, unit));
  150. return new Timeout(t, newUnit);
  151. } else {
  152. return new Timeout((long) inputTime, unit);
  153. }
  154. }
  155. }
  156. private static Matcher matcher(String pattern, String valStr) {
  157. return Pattern.compile(pattern).matcher(valStr);
  158. }
  159. private static boolean anyOf(String a, String... cases) {
  160. for (String b : cases) {
  161. if (StringUtils.equalsIgnoreCase(a, b))
  162. return true;
  163. }
  164. return false;
  165. }
  166. private static IllegalArgumentException notTimeUnit(String section,
  167. String subsection, String name, String valueString) {
  168. String key = section
  169. + (subsection != null ? "." + subsection : "")
  170. + "." + name;
  171. return new IllegalArgumentException(MessageFormat.format(
  172. DhtText.get().notTimeUnit, key, valueString));
  173. }
  174. private final long time;
  175. private final TimeUnit unit;
  176. /**
  177. * Construct a new timeout.
  178. *
  179. * @param time
  180. * how long to wait.
  181. * @param unit
  182. * the unit that {@code time} was expressed in.
  183. */
  184. public Timeout(long time, TimeUnit unit) {
  185. this.time = time;
  186. this.unit = unit;
  187. }
  188. /** @return how long to wait, expressed as {@link #getUnit()}s. */
  189. public long getTime() {
  190. return time;
  191. }
  192. /** @return the unit of measure for {@link #getTime()}. */
  193. public TimeUnit getUnit() {
  194. return unit;
  195. }
  196. @Override
  197. public int hashCode() {
  198. return (int) time;
  199. }
  200. @Override
  201. public boolean equals(Object other) {
  202. if (other instanceof Timeout)
  203. return getTime() == ((Timeout) other).getTime()
  204. && getUnit().equals(((Timeout) other).getUnit());
  205. return false;
  206. }
  207. @Override
  208. public String toString() {
  209. return getTime() + " " + getUnit();
  210. }
  211. }