434,711 Members | 2,144 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,711 IT Pros & Developers. It's quick & easy.

need help adding to a dynamic counter

✓ answered by pbrockway2

I'm not sure String is such a good idea. Even if you decide to treat the ip address as a string, the latency is an integral value and should be modelled with an int.

Create an Entry class that includes the information you wish to extract from a line of the file. (like ip address and latency). Then as you read the file, build up a list (eg ArrayList<Entry> etc) of these entries.

Creating the output will be a matter of going through the list and counting the number of times a given latency was exceeded for a given address.

-----

Another approach would be to build up the latency frequencies as you read the file.

The basic data structure here would be a map whose key is the ip address and whose value is a counter which remembers the number of times the latency exceeded various values. Creating the output is easier in this case since all the data will be stored in the map's entry set.

Something like the following might be suggestive:

Expand|Select|Wrap|Line Numbers
1. public class LatencyCounter {
2.
3.     private static final int[] BIN_VALUES = {1000, 2000, 3000, 5000, 30000};
4.
5.         // the counts are the number of times values were
6.         // added that exceeded the bin value and were less
7.         // than or equal to the next
8.     private int[] counts;
9.
10.     public LatencyCounter() {
11.         counts = new int[BIN_VALUES.length];
12.     }
13.
14.         /** Records the fact that a given value was observed. */
15.     public void add(int value) {
16.         for(int ndx = BIN_VALUES.length - 1; ndx >= 0; ndx--) {
17.             if(value > BIN_VALUES[ndx]) {
18.                 counts[ndx]++;
19.                 return;
20.             }
21.         }
22.     }
23.
24.         /**
25.          * Returns the number of times that observed values exceeded a given value.
26.          * TODO this could return the number of observations recorded in the
27.          * bin containing value.
28.          */
29.     public int getExceedCount(int value) {
30.         int ret = 0;
31.         for(int ndx = BIN_VALUES.length - 1; ndx >= 0; ndx--) {
32.             if(BIN_VALUES[ndx] >= value) {
33.                 ret += counts[ndx];
34.             } else {
35.                 break;
36.             }
37.         }
38.         return ret;
39.     }
40. }
41.
42. import java.util.HashMap;
43. import java.util.Map;
44. import java.util.Set;
45.
46. public class LatencyCounterMap {
47.
48.     private Map<String,LatencyCounter> map = new HashMap<String,LatencyCounter>();
49.
50.         /**
51.          * Records the fact that a given ip experienced a latency of a given value.
52.          */
53.     public void add(String ip, int value) {
54.         LatencyCounter counter = map.get(ip);
55.         if(counter == null) {
56.             counter = new LatencyCounter();
57.             map.put(ip, counter);
58.         }
60.     }
61.
62.         /**
63.          * Returns the number of times that observed values exceeded a given value
64.          * for a given ip address.
65.          */
66.     public int getExceedCount(String ip, int value) {
67.         LatencyCounter counter = map.get(ip);
68.         if(counter == null) {
69.             return 0;
70.         }
71.         return counter.getExceedCount(value);
72.     }
73.
74.     public Set<String> getAddressSet() {
75.         return map.keySet();
76.     }
77.
78. }
79.
80.
81. public class LatencyTest {
82.     public static void main(String[] args) {
83.             // a data structure that remembers values from
84.             // lines in the data file
85.         LatencyCounterMap map = new LatencyCounterMap();
86.
87.             // simulate reading the file
96.
97.             // produce some output
98.         for(String ip :map.getAddressSet()) {
99.             System.out.printf(
100.                     "%-17s%4d%4d%4d%4d%4d%n",
101.                     ip, map.getExceedCount(ip, 1000), map.getExceedCount(ip, 2000),
102.                     map.getExceedCount(ip, 3000), map.getExceedCount(ip, 5000),
103.                     map.getExceedCount(ip, 30000));
104.         }
105.     }
106. }
107.
Which gives the following output:

Expand|Select|Wrap|Line Numbers
1. 170.137.94.2        2   0   0   0   0
2. 170.137.94.1        1   0   0   0   0
3. 170.137.94.11       2   1   0   0   0
4. 170.137.94.96       1   0   0   0   0
5. 170.137.94.95       2   1   1   0   0
6.

4 Replies

 100+ P: 112 Also, would this maybe help. I create a two dimensional array. String [][] casArray = new String; I make the first one 200 because I dont know how larger the file will get and how many IP addresses I get, and I just fill the ends with a random letter that isnt used. Storage space is not a problem. The first Array holds the IPaddresses and the second array holds the duration. Since its in a file its saved as a string initially so I think this works. Then I sort the array. I then check to see if the current index for the IPaddress array equals the next index's IPaddress, and if it does, I check the next one and so on. When I find one that doesnt match, I go back to the previous ones to get the duration, check to see what ranges they fall in and increment the counter. I record this and set the counters back to 0 and then go on from where I left and do this over again till I hit the end of the array. I dont know how and if this works and if it even sounds good. I am just stuck and trying to figure this out. If this does work, can I get an example of this codewise because I am pretty lost when it comes to 2 dimensional arrays. I am new to java so that is why i am using a list. I can use hash tables, maps anything. Please help. thanks Nov 6 '09 #2 