Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RandInt |
|
| 3.5;3.5 |
1 | /* | |
2 | * Copyright 2010 LinkedIn, Inc | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not | |
5 | * use this file except in compliance with the License. You may obtain a copy of | |
6 | * the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |
12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |
13 | * License for the specific language governing permissions and limitations under | |
14 | * the License. | |
15 | */ | |
16 | ||
17 | package datafu.pig.numbers; | |
18 | ||
19 | import java.io.IOException; | |
20 | import java.util.Random; | |
21 | ||
22 | import org.apache.pig.data.DataType; | |
23 | import org.apache.pig.impl.logicalLayer.schema.Schema; | |
24 | ||
25 | import datafu.pig.util.SimpleEvalFunc; | |
26 | ||
27 | /** | |
28 | * Generates a uniformly distributed integer between two bounds | |
29 | */ | |
30 | 451 | public class RandInt extends SimpleEvalFunc<Integer> |
31 | { | |
32 | 451 | private final Random rand = new Random(); |
33 | ||
34 | /** | |
35 | * @param min lower bound for random number | |
36 | * @param max upper bound for random number | |
37 | */ | |
38 | public Integer call(Integer min, Integer max) throws IOException | |
39 | { | |
40 | try | |
41 | { | |
42 | 100 | if (min > max) |
43 | { | |
44 | 0 | throw new RuntimeException("The first argument must be less than the second"); |
45 | } | |
46 | 100 | return rand.nextInt(max - min + 1) + min; |
47 | } | |
48 | 0 | catch (Exception e) { |
49 | 0 | throw new IOException(e); |
50 | } | |
51 | } | |
52 | ||
53 | @Override | |
54 | public Schema outputSchema(Schema input) | |
55 | { | |
56 | 41 | return new Schema(new Schema.FieldSchema("rand", DataType.INTEGER)); |
57 | } | |
58 | ||
59 | } | |
60 |