I'm making an Android app that has barcode scanning, and when I get a result back it comes back as an instance of the Barcode
class. It has two values I'm interested in for the case of this question: barcode.format
and barcode.valueFormat
Both of these values are integers, and as far as I can tell, these integers are matched up to what basically is (but isn't) an Enum as static fields in the Barcode class definition.
So for illustration purposes, you've got the class definition of Barcode like so:
public class Barcode extends AbstractSafeParcelable {
...
public static final int ALL_FORMATS = 0;
public static final int CODE_128 = 1;
public static final int CODE_39 = 2;
public static final int CODE_93 = 4;
public static final int CODABAR = 8;
public static final int DATA_MATRIX = 16;
public static final int EAN_13 = 32;
public static final int EAN_8 = 64;
public static final int ITF = 128;
public static final int QR_CODE = 256;
public static final int UPC_A = 512;
public static final int UPC_E = 1024;
public static final int PDF417 = 2048;
public static final int AZTEC = 4096;
public static final int CONTACT_INFO = 1;
public static final int EMAIL = 2;
public static final int ISBN = 3;
public static final int PHONE = 4;
public static final int PRODUCT = 5;
public static final int SMS = 6;
public static final int TEXT = 7;
public static final int URL = 8;
public static final int WIFI = 9;
public static final int GEO = 10;
public static final int CALENDAR_EVENT = 11;
public static final int DRIVER_LICENSE = 12;
...
And the barcode
instance may come back with a barcode.format
of 32 and a barcode.valueFormat
of 5.
So on my end, what I would want is to take those numbers have have a string of 'EAN_13 : PRODUCT'
, but at a glance, these public static final
fields don't make it easy to get the matching string values out.
I could of course make a Map<int, string>
with all the known types shown here, but I don't like that idea because if any future formats get added, my Map
will be out of date. And I don't like the idea of duplicating information that already exists as well.
So how do I convert these ints to their matching String values in a clean, future-proof way in Java?