Realm implements Serializable, Comparable<Realm> { public static Realm register(@NonNull String realmId, @NonNull String secondLevelDomain) { realmId = realmId.toLowerCase(Locale.US); secondLevelDomain = secondLevelDomain.toLowerCase(Locale.US); synchronized (KNOWN_REALMS) { for (Realm realm : Realm.values()) { if (realm.realmId.equals(realmId)) { if (!realm.secondLevelDomain.equals(secondLevelDomain)) { throw new IllegalArgumentException( "RealmId : " + realmId + " is already registered with " + realm.getSecondLevelDomain() + ". It cannot be re-registered with a different secondLevelDomain"); } return realm; } } return new Realm(realmId, secondLevelDomain); } } private  Realm(@NonNull String realmId, @NonNull String secondLevelDomain); int compareTo(Realm other); @Override // For backward compatibility maintain the enum toString behavior String toString(); static Realm[] values(); static Realm valueOf(@NonNull String name); static Realm register(@NonNull String realmId, @NonNull String secondLevelDomain); static final Realm OC1; static final Realm OC2; static final Realm OC3; static final Realm OC4; static final Realm OC8; }
@Test(expected = IllegalArgumentException.class) public void existingRealmsAreImmutable() { Realm.register(Realm.OC1.getRealmId(), NEW_SECOND_LEVEL_DOMAIN); }
@Test(expected = IllegalArgumentException.class) public void newRealmsAreImmutable() { final String newRealmId = "foo1"; Realm.register(newRealmId, NEW_SECOND_LEVEL_DOMAIN); Realm.register(newRealmId, Realm.OC1.getSecondLevelDomain()); }