blob: f937e5076e1470cc33c6f7c9409c79193d74546e [file] [log] [blame]
Jonathan Hart6df90172014-04-03 10:13:11 -07001package net.onrc.onos.core.datastore.utils;
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -08002
3import java.nio.ByteBuffer;
4import java.util.Comparator;
5
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -07006/**
7 * Comparator which will compares the content of byte[].
8 *
9 * Expected to be used with TreeMap, etc. when you want to use byte[] as a key.
10 */
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -080011public final class ByteArrayComparator implements Comparator<byte[]> {
12
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070013 /**
14 * Instance which can be used, if you want to avoid instantiation per Map.
15 */
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -080016 public static final ByteArrayComparator BYTEARRAY_COMPARATOR = new ByteArrayComparator();
17
18 @Override
Yuta HIGUCHI66ca1bf2014-03-12 18:34:09 -070019 public int compare(final byte[] o1, final byte[] o2) {
Yuta HIGUCHI826b4a42014-03-24 13:10:33 -070020 final ByteBuffer b1 = ByteBuffer.wrap(o1);
21 final ByteBuffer b2 = ByteBuffer.wrap(o2);
22 return b1.compareTo(b2);
Yuta HIGUCHI1ef85c42014-01-29 17:23:21 -080023 }
24}