1 /*
2  * Copyright (C) 2011 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.google.common.cache;
16 
17 /**
18  * Utility {@link Weigher} implementations intended for use in testing.
19  *
20  * @author Charles Fry
21  */
22 public class TestingWeighers {
23 
24   /** Returns a {@link Weigher} that returns the given {@code constant} for every request. */
constantWeigher(int constant)25   static Weigher<Object, Object> constantWeigher(int constant) {
26     return new ConstantWeigher(constant);
27   }
28 
29   /** Returns a {@link Weigher} that uses the integer key as the weight. */
intKeyWeigher()30   static Weigher<Integer, Object> intKeyWeigher() {
31     return new IntKeyWeigher();
32   }
33 
34   /** Returns a {@link Weigher} that uses the integer value as the weight. */
intValueWeigher()35   static Weigher<Object, Integer> intValueWeigher() {
36     return new IntValueWeigher();
37   }
38 
39   static final class ConstantWeigher implements Weigher<Object, Object> {
40     private final int constant;
41 
ConstantWeigher(int constant)42     ConstantWeigher(int constant) {
43       this.constant = constant;
44     }
45 
46     @Override
weigh(Object key, Object value)47     public int weigh(Object key, Object value) {
48       return constant;
49     }
50   }
51 
52   static final class IntKeyWeigher implements Weigher<Integer, Object> {
53     @Override
weigh(Integer key, Object value)54     public int weigh(Integer key, Object value) {
55       return key;
56     }
57   }
58 
59   static final class IntValueWeigher implements Weigher<Object, Integer> {
60     @Override
weigh(Object key, Integer value)61     public int weigh(Object key, Integer value) {
62       return value;
63     }
64   }
65 }
66