UuidAwareBytesDataType.java

  1. /*
  2.  *
  3.  * The DbUnit Database Testing Framework
  4.  * Copyright (C)2002-2008, DbUnit.org
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2.1 of the License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with this library; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  *
  20.  */

  21. package org.dbunit.dataset.datatype;

  22. import java.util.UUID;
  23. import java.util.regex.Matcher;
  24. import java.util.regex.Pattern;

  25. /**
  26.  * <p>
  27.  * A datatype that is capable of storing UUIDs into BINARY fields (big-endian).
  28.  * </p>
  29.  * <p>
  30.  * For the UUID to be detected as such, the string value of the field has to be
  31.  * in the form of {@code uuid'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'}, where the
  32.  * x's are the actual string value of the UUID, hex-encoded. Example:
  33.  * </p>
  34.  *
  35.  * <pre>
  36.  *     &lt;company id="uuid'791ae85a-d8d0-11e2-8c43-50e549c9b654'" name="ACME"/&gt;
  37.  * </pre>
  38.  *
  39.  * @author Timur Strekalov
  40.  */
  41. public class UuidAwareBytesDataType extends BytesDataType
  42. {
  43.     /**
  44.      * The regular expression for a hexadecimal UUID representation.
  45.      */
  46.     private static final Pattern UUID_RE = Pattern.compile(
  47.             "uuid'([0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12})'");

  48.     UuidAwareBytesDataType(final String name, final int sqlType)
  49.     {
  50.         super(name, sqlType);
  51.     }

  52.     @Override
  53.     public Object typeCast(final Object value) throws TypeCastException
  54.     {
  55.         return super.typeCast(uuidAwareValueOf(value));
  56.     }

  57.     private static Object uuidAwareValueOf(final Object value)
  58.     {
  59.         if (value instanceof String)
  60.         {
  61.             final String s = (String) value;
  62.             final Matcher m = UUID_RE.matcher(s);

  63.             if (m.find())
  64.             {
  65.                 final UUID uuid = UUID.fromString(m.group(1));
  66.                 return uuidToBytes(uuid);
  67.             }
  68.         }

  69.         return value;
  70.     }

  71.     private static byte[] uuidToBytes(final UUID uuid)
  72.     {
  73.         final long msb = uuid.getMostSignificantBits();
  74.         final long lsb = uuid.getLeastSignificantBits();

  75.         return new byte[] {extractByte(msb, 0), extractByte(msb, 1),
  76.                 extractByte(msb, 2), extractByte(msb, 3), extractByte(msb, 4),
  77.                 extractByte(msb, 5), extractByte(msb, 6), extractByte(msb, 7),
  78.                 extractByte(lsb, 0), extractByte(lsb, 1), extractByte(lsb, 2),
  79.                 extractByte(lsb, 3), extractByte(lsb, 4), extractByte(lsb, 5),
  80.                 extractByte(lsb, 6), extractByte(lsb, 7)};
  81.     }

  82.     private static byte extractByte(final long value, final int byteIndex)
  83.     {
  84.         return (byte) (value >> (56 - byteIndex * 8) & 0xff);
  85.     }
  86. }