Make account formatter available to plugins
This allows plugins to use the same code as Gerrit core to format account names. Change-Id: I5331a2f543c3d5a592420c671347b1f9214af486 Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
// Copyright (C) 2015 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.client;
|
||||
|
||||
import com.google.gerrit.client.info.AccountInfo;
|
||||
|
||||
public class AccountFormatter {
|
||||
private final String anonymousCowardName;
|
||||
|
||||
public AccountFormatter(String anonymousCowardName) {
|
||||
this.anonymousCowardName = anonymousCowardName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats an account as a name and an email address.
|
||||
* <p>
|
||||
* Example output:
|
||||
* <ul>
|
||||
* <li>{@code A U. Thor <author@example.com>}: full populated</li>
|
||||
* <li>{@code A U. Thor (12)}: missing email address</li>
|
||||
* <li>{@code Anonymous Coward <author@example.com>}: missing name</li>
|
||||
* <li>{@code Anonymous Coward (12)}: missing name and email address</li>
|
||||
* </ul>
|
||||
*/
|
||||
public String nameEmail(AccountInfo info) {
|
||||
String name = info.name();
|
||||
if (name == null || name.trim().isEmpty()) {
|
||||
name = anonymousCowardName;
|
||||
}
|
||||
|
||||
StringBuilder b = new StringBuilder().append(name);
|
||||
if (info.email() != null) {
|
||||
b.append(" <").append(info.email()).append(">");
|
||||
} else if (info._accountId() > 0) {
|
||||
b.append(" (").append(info._accountId()).append(")");
|
||||
}
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats an account name.
|
||||
* <p>
|
||||
* If the account has a full name, it returns only the full name. Otherwise it
|
||||
* returns a longer form that includes the email address.
|
||||
*/
|
||||
public String name(AccountInfo ai) {
|
||||
if (ai.name() != null && !ai.name().trim().isEmpty()) {
|
||||
return ai.name();
|
||||
}
|
||||
String email = ai.email();
|
||||
if (email != null) {
|
||||
int at = email.indexOf('@');
|
||||
return 0 < at ? email.substring(0, at) : email;
|
||||
}
|
||||
return nameEmail(ai);
|
||||
}
|
||||
}
|
@@ -69,18 +69,7 @@ public class FormatUtil {
|
||||
* </ul>
|
||||
*/
|
||||
public static String nameEmail(AccountInfo info) {
|
||||
String name = info.name();
|
||||
if (name == null || name.trim().isEmpty()) {
|
||||
name = Gerrit.info().user().anonymousCowardName();
|
||||
}
|
||||
|
||||
StringBuilder b = new StringBuilder().append(name);
|
||||
if (info.email() != null) {
|
||||
b.append(" <").append(info.email()).append(">");
|
||||
} else if (info._accountId() > 0) {
|
||||
b.append(" (").append(info._accountId()).append(")");
|
||||
}
|
||||
return b.toString();
|
||||
return createAccountFormatter().nameEmail(info);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,16 +88,8 @@ public class FormatUtil {
|
||||
* If the account has a full name, it returns only the full name. Otherwise it
|
||||
* returns a longer form that includes the email address.
|
||||
*/
|
||||
public static String name(AccountInfo ai) {
|
||||
if (ai.name() != null && !ai.name().trim().isEmpty()) {
|
||||
return ai.name();
|
||||
}
|
||||
String email = ai.email();
|
||||
if (email != null) {
|
||||
int at = email.indexOf('@');
|
||||
return 0 < at ? email.substring(0, at) : email;
|
||||
}
|
||||
return nameEmail(ai);
|
||||
public static String name(AccountInfo info) {
|
||||
return createAccountFormatter().name(info);
|
||||
}
|
||||
|
||||
public static AccountInfo asInfo(Account acct) {
|
||||
@@ -132,4 +113,8 @@ public class FormatUtil {
|
||||
acct.getPreferredEmail(),
|
||||
acct.getUsername());
|
||||
}
|
||||
|
||||
private static AccountFormatter createAccountFormatter() {
|
||||
return new AccountFormatter(Gerrit.info().user().anonymousCowardName());
|
||||
}
|
||||
}
|
||||
|
@@ -14,11 +14,17 @@
|
||||
|
||||
package com.google.gerrit.plugin.client;
|
||||
|
||||
import com.google.gerrit.client.AccountFormatter;
|
||||
import com.google.gerrit.client.DateFormatter;
|
||||
import com.google.gerrit.client.info.AccountInfo;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class FormatUtil {
|
||||
private final static AccountFormatter accountFormatter =
|
||||
new AccountFormatter(Plugin.get().getServerInfo().user()
|
||||
.anonymousCowardName());
|
||||
|
||||
/** Format a date using a really short format. */
|
||||
public static String shortFormat(Date dt) {
|
||||
return createDateFormatter().shortFormat(dt);
|
||||
@@ -37,4 +43,29 @@ public class FormatUtil {
|
||||
private static DateFormatter createDateFormatter() {
|
||||
return new DateFormatter(Plugin.get().getUserPreferences());
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats an account as a name and an email address.
|
||||
* <p>
|
||||
* Example output:
|
||||
* <ul>
|
||||
* <li>{@code A U. Thor <author@example.com>}: full populated</li>
|
||||
* <li>{@code A U. Thor (12)}: missing email address</li>
|
||||
* <li>{@code Anonymous Coward <author@example.com>}: missing name</li>
|
||||
* <li>{@code Anonymous Coward (12)}: missing name and email address</li>
|
||||
* </ul>
|
||||
*/
|
||||
public static String nameEmail(AccountInfo info) {
|
||||
return accountFormatter.nameEmail(info);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats an account name.
|
||||
* <p>
|
||||
* If the account has a full name, it returns only the full name. Otherwise it
|
||||
* returns a longer form that includes the email address.
|
||||
*/
|
||||
public static String name(AccountInfo info) {
|
||||
return accountFormatter.name(info);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user