aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml/docilealligator/infinityforreddit/videoautoplay/CacheManager.java
blob: 4e61485c3b3660a15c5660f1d8d7981a95d9a06b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
 * Copyright (c) 2017 Nam Nguyen, nam@ene.im
 *
 * 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 ml.docilealligator.infinityforreddit.videoautoplay;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import java.util.LinkedHashMap;

import ml.docilealligator.infinityforreddit.videoautoplay.widget.Container;

/**
 * {@link CacheManager} is a helper interface used by {@link Container} to manage the
 * {@link PlaybackInfo} of {@link ToroPlayer}s. For each {@link ToroPlayer},
 * {@link CacheManager} will ask for a unique key for its {@link PlaybackInfo} cache.
 * {@link Container} uses a {@link LinkedHashMap} to implement the caching mechanism, so
 * {@link CacheManager} must provide keys which are uniquely distinguished by
 * {@link Object#equals(Object)}.
 *
 * @author eneim (7/5/17).
 */
public interface CacheManager {

  /**
   * Get the unique key for the {@link ToroPlayer} of a specific order. Note that this key must
   * also be managed by {@link RecyclerView.Adapter} so that it prevents the uniqueness at data
   * change events.
   *
   * @param order order of the {@link ToroPlayer}.
   * @return the unique key of the {@link ToroPlayer}.
   */
  @Nullable Object getKeyForOrder(int order);

  /**
   * Get the order of a specific key value. Returning a {@code null} order value here will tell
   * {@link Container} to ignore this key's cache order.
   *
   * @param key the key value to lookup.
   * @return the order of the {@link ToroPlayer} whose unique key value is key.
   */
  @Nullable Integer getOrderForKey(@NonNull Object key);

  /**
   * A built-in {@link CacheManager} that use the order as the unique key. Note that this is not
   * data-changes-proof. Which means that after data change events, the map may need to be
   * updated.
   */
  CacheManager DEFAULT = new CacheManager() {
    @Override public Object getKeyForOrder(int order) {
      return order;
    }

    @Override public Integer getOrderForKey(@NonNull Object key) {
      return key instanceof Integer ? (Integer) key : null;
    }
  };
}