Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / apphosting / base / AppId.java
blobf1dcdf48dd94b73d370c4bbc547d0e1d28cd2d12
1 // Copyright 2011 Google Inc. All Rights Reserved.
3 package com.google.apphosting.base;
5 import java.util.regex.Matcher;
6 import java.util.regex.Pattern;
8 /**
9 * Class used for parsing the various components of the AppId.
12 public class AppId {
13 private static final int APP_ID_MAX_LEN = 100;
14 private static final Pattern DISPLAY_APP_ID_RE =
15 Pattern.compile("[a-z\\d\\-]{1," + APP_ID_MAX_LEN + "}", Pattern.CASE_INSENSITIVE);
16 private static final Pattern DOMAIN_RE =
17 Pattern.compile("([a-z\\d\\-\\.]{1," + APP_ID_MAX_LEN + "})?\\:", Pattern.CASE_INSENSITIVE);
18 private static final Pattern PARTITION_RE =
19 Pattern.compile("([a-z\\d\\-]{1," + APP_ID_MAX_LEN + "})?\\~", Pattern.CASE_INSENSITIVE);
20 private static final Pattern APP_ID_RE =
21 Pattern.compile(
22 "(?:" + PARTITION_RE + ")?((?:" + DOMAIN_RE + ")?(" + DISPLAY_APP_ID_RE + "))",
23 Pattern.CASE_INSENSITIVE);
24 private String appId;
25 private String domain;
26 private String longAppId;
27 private String displayAppId;
28 private String partition;
30 /**
31 * Create a new AppId based on an appId formatted as follows:
33 * [(partition)~][(domain):](display-app-id)
35 * @param appId The appId to parse.
37 private AppId(String appId) {
38 this.appId = appId;
39 if (appId == null || appId.length() == 0) {
40 return;
42 Matcher matcher = APP_ID_RE.matcher(appId);
43 if (!matcher.matches()) {
44 return;
46 this.partition = matcher.group(1);
47 this.longAppId = matcher.group(2);
48 this.domain = matcher.group(3);
49 this.displayAppId = matcher.group(4);
52 /**
53 * @return The full appId.
55 public String getAppId() {
56 return appId;
59 /**
60 * @return The domain component of this appId.
62 public String getDomain() {
63 return domain;
66 /**
67 * @return The display-app-id component of this appId.
69 public String getDisplayAppId() {
70 return displayAppId;
73 /**
74 * @return The partition component of the appId.
76 public String getPartition() {
77 return partition;
80 /**
81 * @return The appId without the partition component.
83 public String getLongAppId() {
84 return longAppId;
87 /**
88 * Returns a new AppId object based on an appId formatted as follows:
90 * [(partition)~][(domain):](display-app-id)
92 * @param appId The appId to parse.
94 * @return AppId object with the parsed appid components.
96 public static AppId parse(String appId) {
97 return new AppId(appId);