forked from codesquad-members-2021/java-was
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from sanhee/refactor/attribute/main
Attribute 리팩토링
- Loading branch information
Showing
10 changed files
with
360 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package webserver.http.attribute; | ||
|
||
import util.HttpRequestUtils; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.StringJoiner; | ||
|
||
public class Attributes { | ||
private final Map<String, String> attributes = new LinkedHashMap<>(); | ||
|
||
public static Attributes from(Map<String, String> attributes) { | ||
return new Attributes().addAll(attributes); | ||
} | ||
|
||
public static Attributes from(String headerText) { | ||
Map<String, String> attributes = new LinkedHashMap<>(); | ||
|
||
String[] splittedHeaderTexts = headerText.split(System.lineSeparator()); | ||
for (String splittedHeaderText : splittedHeaderTexts) { | ||
HttpRequestUtils.Pair pair = HttpRequestUtils.parseHeader(splittedHeaderText); | ||
|
||
if (pair != null) { | ||
attributes.put(pair.getKey(), pair.getValue()); | ||
} | ||
} | ||
|
||
return from(attributes); | ||
} | ||
|
||
public Attributes add(String key, String value) { | ||
attributes.put(key, value); | ||
return this; | ||
} | ||
|
||
public Attributes addAll(Map<String, String> attributes) { | ||
this.attributes.putAll(attributes); | ||
return this; | ||
} | ||
|
||
public String get(String key) { | ||
return attributes.get(key); | ||
} | ||
|
||
public String getOrDefault(String key, String defaultValue) { | ||
return attributes.getOrDefault(key, defaultValue); | ||
} | ||
|
||
public String toHeaderText() { | ||
StringJoiner headerText = new StringJoiner("\r\n"); | ||
for (Map.Entry<String, String> each : attributes.entrySet()) { | ||
headerText.add(each.getKey() + ": " + each.getValue()); | ||
} | ||
|
||
return headerText.toString(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Attributes that = (Attributes) o; | ||
return Objects.equals(attributes, that.attributes); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(attributes); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.