/hudson-utils/src/main/java/org/hudsonci/utils/common/VersionFormatter.java

http://github.com/hudson/hudson · Java · 83 lines · 36 code · 9 blank · 38 comment · 9 complexity · e2bc5f07be3e92b0719aec250f094afe MD5 · raw file

  1. /**
  2. * The MIT License
  3. *
  4. * Copyright (c) 2010-2011 Sonatype, Inc. All rights reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package org.hudsonci.utils.common;
  25. /**
  26. * Utility supplying different ways to format a version number.
  27. *
  28. * @author Jamie Whitehouse
  29. * @since 2.1.0
  30. */
  31. class VersionFormatter
  32. {
  33. /**
  34. * @return a formatted version number containing the raw information
  35. */
  36. public static String asRawVersion( String version, String timestamp, String sequence )
  37. {
  38. StringBuilder buff = new StringBuilder();
  39. if ( version == null )
  40. {
  41. version = VersionSupport.UNKNOWN;
  42. }
  43. buff.append( version );
  44. if ( timestamp != null )
  45. {
  46. buff.append( "," ).append( timestamp );
  47. }
  48. if ( sequence != null )
  49. {
  50. buff.append( "#" ).append( sequence );
  51. }
  52. return buff.toString();
  53. }
  54. /**
  55. * Generates an appropriate format based on the project being a release or development version. Example:
  56. * 0.2.1,201009291818#1
  57. *
  58. * @return a formatted version number in the simplest and most significant form
  59. */
  60. public static String asCanonicalVersion( String version, String timestamp, String sequence )
  61. {
  62. String derivedVersion = version;
  63. if ( derivedVersion == null )
  64. {
  65. derivedVersion = VersionSupport.UNKNOWN;
  66. }
  67. String snapshot = "-SNAPSHOT";
  68. if ( derivedVersion.contains( snapshot ) )
  69. {
  70. derivedVersion = asRawVersion( version.replace( snapshot, "" ), timestamp, sequence );
  71. }
  72. return derivedVersion;
  73. }
  74. }