package com.sbandara.cloudpokes;

import javax.xml.bind.DatatypeConverter;

public class DeviceToken {
	
	private final byte[] apns_token;
	private final String gcm_token;

	public static DeviceToken apnsToken(byte[] apns_token) {
		return new DeviceToken(apns_token);
	}
	
	public static DeviceToken apnsTokenFromBase64(String apns_token) {
		byte[] token = DatatypeConverter.parseBase64Binary(apns_token);
		if (token.length != 32) {
			throw new IllegalArgumentException("Not an APNS token.");
		}
		return new DeviceToken(token);
	}

	DeviceToken(byte[] apns_token) {
		this.apns_token = apns_token;
		gcm_token = null;
	}

	public final static DeviceToken gcmToken(String gcm_token) {
		return new DeviceToken(gcm_token);
	}

	DeviceToken(String gcm_token) {
		apns_token = null;
		this.gcm_token = gcm_token;
	}
	
	public final boolean isGcmToken() { return gcm_token != null; }
	
	public final boolean isApnsToken() { return apns_token != null; }
	
	public final String getGcmToken() {
		if (gcm_token == null) {
			throw new UnsupportedOperationException("Not a GCM token.");
		}
		return gcm_token;
	}

	public final byte[] getApnsToken() {
		if (apns_token == null) {
			throw new UnsupportedOperationException("Not an APNS token.");
		}
		return apns_token;
	}
}

@@ -941,32 +941,1171 @@
 en != null; %7D%0A%09%0A
+%09public final boolean equalsApnsToken(byte%5B%5D token) %7B%0A%09%09if ((this.apns_token == null) %7C%7C (token == null)) %7B%0A%09%09%09return false;%0A%09%09%7D%0A%09%09for (int k = 0; k %3C this.apns_token.length; k ++) %7B%0A%09%09%09if (this.apns_token%5Bk%5D != token%5Bk%5D) %7B%0A%09%09%09%09return false;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return true;%0A%09%7D%0A%09%0A%09public final boolean equalsApnsToken(String base64) %7B%0A%09%09if (base64 == null) %7B%0A%09%09%09return false;%0A%09%09%7D%0A%09%09return equalsApnsToken(DatatypeConverter.parseBase64Binary(base64));%0A%09%7D%0A%09%0A%09public final boolean equalsGcmToken(String token) %7B%0A%09%09if (token == null) %7B%0A%09%09%09return false;%0A%09%09%7D%0A%09%09return token.equals(this.gcm_token);%0A%09%7D%0A%09%0A%09@Override%0A%09public final boolean equals(Object anObject) %7B%0A%09%09if (! (anObject instanceof DeviceToken)) %7B%0A%09%09%09return false;%0A%09%09%7D%0A%09%09if (this == anObject) %7B%0A%09%09%09return true;%0A%09%09%7D%0A%09%09DeviceToken other = (DeviceToken) anObject;%0A%09%09if (other.gcm_token != null) %7B%0A%09%09%09return other.gcm_token.equals(this.gcm_token);%0A%09%09%7D%0A%09%09else return equalsApnsToken(other.apns_token);%0A%09%7D%0A%09%0A%09@Override%0A%09public final String toString() %7B%0A%09%09if (gcm_token != null) %7B%0A%09%09%09return %22GCM:%22 + gcm_token;%0A%09%09%7D%0A%09%09else %7B%0A%09%09%09return %22APNS:%22 + DatatypeConverter.printBase64Binary(apns_token);%0A%09%09%7D%0A%09%7D%0A%09%0A
 %09public final St
@@ -2404,10 +2404,215 @@
 ken;%0A%09%7D%0A
+%09%0A%09public final String getBase64ApnsToken() %7B%0A%09%09if (apns_token == null) %7B%0A%09%09%09throw new UnsupportedOperationException(%22Not an APNS token.%22);%0A%09%09%7D%0A%09%09return DatatypeConverter.printBase64Binary(apns_token);%0A%09%7D%0A
 %7D%0A

Add equality and string methods.