AccountSshKey: Split common getter code to utility method

Change-Id: I39b566890da209381bcfc7ac01596a4d4276e495
diff --git a/gerrit-reviewdb/src/main/java/com/google/gerrit/reviewdb/client/AccountSshKey.java b/gerrit-reviewdb/src/main/java/com/google/gerrit/reviewdb/client/AccountSshKey.java
index a28fc60..f185a76 100644
--- a/gerrit-reviewdb/src/main/java/com/google/gerrit/reviewdb/client/AccountSshKey.java
+++ b/gerrit-reviewdb/src/main/java/com/google/gerrit/reviewdb/client/AccountSshKey.java
@@ -89,43 +89,27 @@
     return sshPublicKey;
   }
 
-  public String getAlgorithm() {
-    final String s = getSshPublicKey();
-    if (s == null || s.length() == 0) {
-      return "none";
+  private String getPublicKeyPart(int index, String defaultValue) {
+    String s = getSshPublicKey();
+    if (s != null && s.length() > 0) {
+      String[] parts = s.split(" ");
+      if (parts.length > index) {
+        return parts[index];
+      }
     }
+    return defaultValue;
+  }
 
-    final String[] parts = s.split(" ");
-    if (parts.length < 1) {
-      return "none";
-    }
-    return parts[0];
+  public String getAlgorithm() {
+    return getPublicKeyPart(0, "none");
   }
 
   public String getEncodedKey() {
-    final String s = getSshPublicKey();
-    if (s == null || s.length() == 0) {
-      return null;
-    }
-
-    final String[] parts = s.split(" ");
-    if (parts.length < 2) {
-      return null;
-    }
-    return parts[1];
+    return getPublicKeyPart(1, null);
   }
 
   public String getComment() {
-    final String s = getSshPublicKey();
-    if (s == null || s.length() == 0) {
-      return "";
-    }
-
-    final String[] parts = s.split(" ", 3);
-    if (parts.length < 3) {
-      return "";
-    }
-    return parts[2];
+    return getPublicKeyPart(2, "");
   }
 
   public boolean isValid() {
diff --git a/gerrit-reviewdb/src/test/java/com/google/gerrit/reviewdb/client/AccountSshKeyTest.java b/gerrit-reviewdb/src/test/java/com/google/gerrit/reviewdb/client/AccountSshKeyTest.java
index 06d5b90..139d360 100644
--- a/gerrit-reviewdb/src/test/java/com/google/gerrit/reviewdb/client/AccountSshKeyTest.java
+++ b/gerrit-reviewdb/src/test/java/com/google/gerrit/reviewdb/client/AccountSshKeyTest.java
@@ -25,9 +25,10 @@
       + "vf8IZixgjCmiBhaL2gt3wff6pP+NXJpTSA4aeWE5DfNK5tZlxlSxqkKOS8JRSUeNQov5T"
       + "w== john.doe@example.com";
 
+  private final Account.Id accountId = new Account.Id(1);
+
   @Test
   public void testValidity() throws Exception {
-    Account.Id accountId = new Account.Id(1);
     AccountSshKey key = new AccountSshKey(
         new AccountSshKey.Id(accountId, -1), KEY);
     assertThat(key.isValid()).isFalse();
@@ -36,4 +37,14 @@
     key = new AccountSshKey(new AccountSshKey.Id(accountId, 1), KEY);
     assertThat(key.isValid()).isTrue();
   }
+
+  @Test
+  public void testGetters() throws Exception {
+    AccountSshKey key = new AccountSshKey(
+        new AccountSshKey.Id(accountId, 1), KEY);
+    assertThat(key.getSshPublicKey()).isEqualTo(KEY);
+    assertThat(key.getAlgorithm()).isEqualTo(KEY.split(" ")[0]);
+    assertThat(key.getEncodedKey()).isEqualTo(KEY.split(" ")[1]);
+    assertThat(key.getComment()).isEqualTo(KEY.split(" ")[2]);
+  }
 }