2

Consider for exemple selection of days of the week, where we can select one day, several days or nothing.
I am trying to store that data as follows:

public static final int NONE = 0b0000000;
public static final int MONDAY = 0b0000001;
public static final int TUESDAY = 0b0000010;
public static final int WEDNESDAY = 0b0000100;
public static final int THURSDAY = 0b0001000;
public static final int FRIDAY = 0b0010000;
public static final int SATURDAY = 0b0100000;
public static final int SUNDAY = 0b1000000;

where 0 is NONE, 11 is MONDAY and TUESDAY, 1000011 is MONDAY, TUESDAY and SUNDAY;
Is there a way, I can write the same in more readable manner?

Edit: Usage of enums has decent performans penalties in dalvik ( android jvm), please do not sugget them.

Yarh
  • 4,459
  • 5
  • 45
  • 95
  • There are several solutions; using an EnumSet or a BitSet, for instance. But why do you want them as booleans? – fge Aug 26 '15 at 13:12
  • is keeping the selection in `int` format a mandatory requirement? – S. Pauk Aug 26 '15 at 13:18
  • @fge android developers gratly discourage using enums. And did not know about bitset yet. If bitset is more flexible, example willbe consideres as an appopriate answer. – Yarh Aug 26 '15 at 13:18
  • @SergeyPauk no, its just my poor knowlage about that topic. – Yarh Aug 26 '15 at 13:18
  • @Yarh, then I would go with `EnumSet` otherwise your solution is readable enough (at least to me) – S. Pauk Aug 26 '15 at 13:20
  • hmm, can you please tell me what are you trying to do here, I still don't get how are you intending to use integers – QuakeCore Aug 26 '15 at 13:34
  • @QuakeCore to combine several boolean field into one field – Yarh Aug 26 '15 at 13:42

3 Answers3

2

I think this is similar to what you are trying to do.

    public static final int NONE = 0;
    public static final int MONDAY = 1;
    public static final int TUESDAY = 2;
    public static final int WEDNESDAY = 4;
    public static final int THURSDAY = 8;
    public static final int FRIDAY = 16;
    public static final int SATURDAY = 32;
    public static final int SUNDAY = 64;

public static void main(String[] args) {

    int z = Integer.MAX_VALUE ^ SUNDAY ^ MONDAY;
    for (int x = 1; x <= 64; x = x * 2) {
        System.out.println(z & x);
    }
}

output

0
2
4
8
16
32
0
QuakeCore
  • 1,886
  • 2
  • 15
  • 33
1

In the interest of StackOverflow users who prefer their code to be elegant I will post a version that uses enums.

public enum Day {

    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday;

    /**
     * Demonstrates the simplicity.
     */
    public String shortForm() {
        return name().substring(0, 3);
    }
}

// Elegant EnumSet initialisation.
private static final EnumSet weekEndDays = EnumSet.of(Day.Saturday, Day.Sunday);
public static final Set<Day> weekend = Collections.<Day>unmodifiableSet(weekEndDays);
public static final Set<Day> weekdays = Collections.<Day>unmodifiableSet(EnumSet.complementOf(weekEndDays));

public void test() {
    for (Day day : weekdays) {
        System.out.println(day.toString() + " or " + day.shortForm());
    }

}
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

Here's an example with a BitSet (although EnumSet would be recommended for clarity).

BitSet bits = new BitSet();
// the below is all redundant
// none
bits.set(0, false);
// monday
bits.set(1, false);
// tuesday
bits.set(2, false);
// wed
bits.set(3, false);
// thu
bits.set(4, false);
// fri
bits.set(5, false);
// sat
bits.set(6, false);
// sun
bits.set(7, false);
// EOF redundant part
// mon + tue (last index exclusive)
bits.set(1, 3);
// comparison, tue + wed
BitSet compare = new BitSet();
// tue + wed
compare.set(2, 4);
System.out.println(bits.equals(compare)); // false
System.out.println(bits.intersects(compare)); // true

Note

Enums are "discouraged" in Android for memory consumption, but it's not such a taboo depending on how many you have (see also here).

Alternatively you could use the Calendar constants as indices.

For instance:

bits.set(Calendar.MONDAY)

Community
  • 1
  • 1
Mena
  • 47,782
  • 11
  • 87
  • 106
  • I don't see how the use of `BitSet` improves the readability of the original solution. – S. Pauk Aug 26 '15 at 13:23
  • @SergeyPauk with the `Calendar` constants it would. But I haven't edited that part in, yet. *Now* I have. – Mena Aug 26 '15 at 13:24