aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java
diff options
context:
space:
mode:
authorDominik Schürmann <dominik@dominikschuermann.de>2015-03-16 13:54:01 +0100
committerDominik Schürmann <dominik@dominikschuermann.de>2015-03-16 13:54:01 +0100
commita655664c0bd177278826720dbac6b22f4e9e1cd2 (patch)
tree63e3ea1fd351232f2c0978b78a6681d999914cde /OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java
parent0e71fcd638195003af36f4cf8f3192a381794516 (diff)
downloadopen-keychain-a655664c0bd177278826720dbac6b22f4e9e1cd2.tar.gz
open-keychain-a655664c0bd177278826720dbac6b22f4e9e1cd2.tar.bz2
open-keychain-a655664c0bd177278826720dbac6b22f4e9e1cd2.zip
object oriented split user id
Diffstat (limited to 'OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java')
-rw-r--r--OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java64
1 files changed, 31 insertions, 33 deletions
diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java
index 26375219b..eb1f93a8b 100644
--- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java
+++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/KeyRing.java
@@ -44,7 +44,7 @@ public abstract class KeyRing {
abstract public String getPrimaryUserIdWithFallback() throws PgpKeyNotFoundException;
- public String[] getSplitPrimaryUserIdWithFallback() throws PgpKeyNotFoundException {
+ public UserId getSplitPrimaryUserIdWithFallback() throws PgpKeyNotFoundException {
return splitUserId(getPrimaryUserIdWithFallback());
}
@@ -62,35 +62,21 @@ public abstract class KeyRing {
/**
* Splits userId string into naming part, email part, and comment part
+ * <p/>
+ * User ID matching:
+ * http://fiddle.re/t4p6f
*
* @param userId
- * @return array with naming (0), email (1), comment (2)
+ * @return theParsedUserInfo
*/
- public static String[] splitUserId(String userId) {
- String[] result = new String[]{null, null, null};
-
- if (userId == null || userId.equals("")) {
- return result;
- }
-
- /*
- * User ID matching:
- * http://fiddle.re/t4p6f
- *
- * test cases:
- * "Max Mustermann (this is a comment) <max@example.com>"
- * "Max Mustermann <max@example.com>"
- * "Max Mustermann (this is a comment)"
- * "Max Mustermann [this is nothing]"
- */
- Matcher matcher = USER_ID_PATTERN.matcher(userId);
- if (matcher.matches()) {
- result[0] = matcher.group(1);
- result[1] = matcher.group(3);
- result[2] = matcher.group(2);
+ public static UserId splitUserId(final String userId) {
+ if (!TextUtils.isEmpty(userId)) {
+ final Matcher matcher = USER_ID_PATTERN.matcher(userId);
+ if (matcher.matches()) {
+ return new UserId(matcher.group(1), matcher.group(3), matcher.group(2));
+ }
}
-
- return result;
+ return new UserId(null, null, null);
}
/**
@@ -101,16 +87,28 @@ public abstract class KeyRing {
* @param comment
* @return
*/
- public static String createUserId(String name, String email, String comment) {
- String userId = name; // consider name a required value
- if (userId != null && !TextUtils.isEmpty(comment)) {
- userId += " (" + comment + ")";
+ public static String createUserId(UserId userId) {
+ String userIdString = userId.name; // consider name a required value
+ if (userIdString != null && !TextUtils.isEmpty(userId.comment)) {
+ userIdString += " (" + userId.comment + ")";
}
- if (userId != null && !TextUtils.isEmpty(email)) {
- userId += " <" + email + ">";
+ if (userIdString != null && !TextUtils.isEmpty(userId.email)) {
+ userIdString += " <" + userId.email + ">";
}
- return userId;
+ return userIdString;
+ }
+
+ public static class UserId {
+ public final String name;
+ public final String email;
+ public final String comment;
+
+ public UserId(String name, String email, String comment) {
+ this.name = name;
+ this.email = email;
+ this.comment = comment;
+ }
}
}