"안녕, 아미고!"
"안녕하세요, 다람쥐 선장입니다!"
"오늘 새로운 비밀 임무가 있습니다. 우리 부대 간의 통신을 암호화하려면 자체 URL 단축 서비스를 구현해야 합니다."
"멋지네요! 어, 제 말은, 준비가 되었다는 뜻입니다. 그런데 이게 왜 필요한 거죠?"
"왜요? 병사여, 우리는 적들에게 둘러싸여 있습니다. 통신선을 보호해야 합니다. 임무를 계속하십시오."
"예, 선생님! 단축 URL 서비스를 구현하십시오."
"우리의 비밀 요원 IntelliJ IDEA로 가십시오. 그곳에서 모든 지침을 받을 것입니다."
"끝내겠습니다, 함장님!"
"진행하다."

16
과제
Java Collections, 레벨 6, 레슨 15
Shortener (part 1)
Let's write a Shortener. It will be similar to
Google URL Shortener (https://goo.gl), but we'll extend its functionality and
make it console-based. It will shorten not only links, but also any string.
9
과제
Java Collections, 레벨 6, 레슨 15
Shortener (part 2)
Shortener will support various data storage strategies
(for strings and string identifiers). All these strategies will be inherited from the
StorageStrategy interface. Read more about the Strategy pattern on Wikipedia.
Our repository will rely on two concepts: key and value.
16
과제
Java Collections, 레벨 6, 레슨 15
Shortener (part 3)
Let's return to the Shortener class:
3.1. Add a Long lastId field to it. Initialize it to zero. This field will
hold the identifier of the last string
added to the repository.
3.2. Add a StorageStrategy storageStrategy field that will store the
data storage strategy.
16
과제
Java Collections, 레벨 6, 레슨 15
Shortener (4)
We'll need several helper classes:
4.1. Create a Helper class.
4.1.1. Add a static String generateRandomString() method, which
will generate a random string. Use the SecureRandom
and BigInteger classes to do this. Hint: search for "random string Java" on Google.
16
과제
Java Collections, 레벨 6, 레슨 15
Shortener (5)
Let's write our first repository (storage strategy). Internally, it will
have an ordinary HashMap. We'll keep all the strategies in the strategy package.
5.1. Create a HashMapStorageStrategy class that implements the StorageStrategy interface.
32
과제
Java Collections, 레벨 6, 레슨 15
Shortener (6)
The first strategy is ready. It's time to test it. To do this:
6.1. Create a Solution class if you haven't already done so.
6.2. Add implementations of the static helper methods to the Solution class:
6.2.1. Set<Long> getIds(Shortener shortener, Set<String> strings).
32
과제
Java Collections, 레벨 6, 레슨 15
Shortener (7)
Let's start implementing the second strategy: OurHashMapStorageStrategy. It won't
use the ready-made HashMap from the standard library, but rather will itself be
a collection.
7.1. Understand how the standard HashMap works. Look at its source code or
google for articles about it.
32
과제
Java Collections, 레벨 6, 레슨 15
Shortener (8)
Add and implement a OurHashMapStorageStrategy class, using the Entry class from the
previous subtask. The OurHashMapStorageStrategy class must implement
the StorageStrategy interface.
8.1. Add the following fields to the class:
8.1.1. static final int DEFAULT_INITIAL_CAPACITY = 16.
32
과제
Java Collections, 레벨 6, 레슨 15
Shortener (9)
Let's write another strategy. We'll call it FileStorageStrategy. It will be very similar
to OurHashMapStorageStrategy, but files will be
the buckets. I'm sure you know what I mean by buckets. If not, copy the internal workings
of HashMap.
9.1. Create a FileBucket class in the strategy package.
32
과제
Java Collections, 레벨 6, 레슨 15
Shortener (10)
Create and implement a FileStorageStrategy class. It must:
10.1. Implement the StorageStrategy interface.
10.2. Use FileBucket as a buckets. Hint: The class
must have a FileBucket[] table field.
16
과제
Java Collections, 레벨 6, 레슨 15
Shortener (11)
As you've noticed, getting an identifier using a string takes a lot more
time than getting a string using an identifier. This is expected and is a consequence of
HashMap's implementation. Let's write a fourth strategy:
OurHashBiMapStorageStrategy, which will eliminate this shortcoming.
16
과제
Java Collections, 레벨 6, 레슨 15
Shortener (12)
It's not all that uncommon to need to create a Map that works in two directions (from key to
value, and from value to key). Implementations of such collections
already exist in various third-party libraries. One such is guava from
Google.
12.1. Download and connect the guava library, version 19.0.
16
과제
Java Collections, 레벨 6, 레슨 15
Shortener (13)
Consider another implementation of BiMap — this time from Apache Commons Collections.
13.1. Download and connect Apache Commons Collections 4.0.
13.2. Implement a DualHashBidiMapStorageStrategy. It must:
13.2.1. support the StorageStrategy interface.
32
과제
Java Collections, 레벨 6, 레슨 15
Shortener (14)
We've tested our strategies many times using the Solution class's
testStrategy() method. It's time to use JUnit to write real unit tests.
14.1. Read about unit tests.
14.2. Download and connect the JUnit 4.12 library. Figure out how to use it.
The JUnit library depends on the hamcrest-core library.
16
과제
Java Collections, 레벨 6, 레슨 15
Shortener (15)
We're going to write another test that verifies that using HashBiMapStorageStrategy to get the identifier for a string
can be faster than using
HashMapStorageStrategy.
15.1. Create a SpeedTest class in the tests package.
9
과제
Java Collections, 레벨 6, 레슨 15
Shortener (16)
Stuff you can do on your own (we won't test you on any of these items):
- Add a strategy for working with a database. Google JDBC.
- Make a web service that returns an identifier for any URL or string,
and returns the string for an identifier.