What is this thing

Brief description

In fact, this is a very simple concatenation string class, can play a very limited role. When it comes to String concatenation, it’s easy to imagine that the other two StringBuilder brothers, StirngBuffer, are qualified to join the group. This article is based on JDK1.8

Story background (why write this article, read a happy can read, this paragraph does not know)

Recently, A du was poor, but under the pressure of money, I finally took a private job ~, so I found two gay friends, Youyou and Aoao, I and Youyou began to write back end. As we all know, a system will have a login, a login will have the authentication of the “full set of services”, our project is not unexpected, this is actually nothing, the general system will choose OAuth2 or Jwt these, but I do not! I want to find a new way, the initial intention is good, nothing more than the Token to be able to parse out the current user information, I want to put the user’s basic information and expiration time, these are “_” underscore Mosaic (example: AccountId_AccountName_ExpireTime), and then AES encryption, as a token, so that the current user information, decrypt the string directly, reduce the burden of Redis. Friend so-and-so 2 years development experience, I think this simple job should be competent, he also said that can, then I give him. Until he finished this function, I instinctively to check the code, suddenly see this code, immediately put the next to sleep is sweet friends kick up. Let’s look at the code ~

account = .... ;// The token generation process
StringBuilder sb = new StringBuilder();
sb.append(account.getAccountId());
sb.append("_");
sb.append(account.getAccountName());
sb.append("_")... String tokenStr = aes.decode(sb.toString());Copy the code

The problem is not that serious, except that every time a value is inserted, it appends an “_”, which feels a bit redundant and uncomfortable. Here’s the StringJoiner for this article!

How do you use it? Where are we going to use it

As you can see in the example above, StringJoiner is used when multiple strings are concatenated using the same character. Specific how to use, to ~ adu food

account = .... ;// Replace the token generation process above with the StringJoiner class
StringJoiner sj = new StringJoiner("_"); sj.add(account.getAccountId()); sj.add(account.getAccountName()); . String tokenStr = aes.decode(sj.toString());Copy the code

He is so magical, how internal how to achieve it, step by step to see its source code, let it “all over the body” ~

The constructor


/** new StringJoiner("") >> Call constructor */
public StringJoiner(CharSequence delimiter) {
    this(delimiter, ""."");
}


public StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix) {
    Objects.requireNonNull(prefix, "The prefix must not be null");
    Objects.requireNonNull(delimiter, "The delimiter must not be null");
    Objects.requireNonNull(suffix, "The suffix must not be null");
    // make defensive copies of arguments
    / / pre -
    this.prefix = prefix.toString();
    // Isolate the string
    this.delimiter = delimiter.toString();
    / / rear
    this.suffix = suffix.toString();
    this.emptyValue = this.prefix + this.suffix;
}

Copy the code

So there are two constructors, and you can see it’s pretty simple, just doing some property assignments, nothing we want to see, so let’s go ahead, okay

Member variables

private final String prefix;
private final String delimiter;
private final String suffix;

private StringBuilder value;
Copy the code

Let’s focus on the value field here, and you can see that it’s a StringBuilder. Now let’s look at StringJoiner’s Add method

public StringJoiner add(CharSequence newElement) {
    prepareBuilder().append(newElement);
    return this;
}

private StringBuilder prepareBuilder(a) {
    if(value ! =null) {
        value.append(delimiter);
    } else {
        value = new StringBuilder().append(prefix);
    }
    return value;
}

Copy the code

This code is easier to call, and it uses the value variable in the Add method, so you can see that StringJoiner uses the StringBuilder class internally. So that’s the end of this article, and the next article is going to look at how it’s implemented in JDK11, in StringJoiner.

The end ~