java/copy-image-resizer/src/cz/frantovo/copyImageResizer/Counters.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 17 Nov 2014 20:13:15 +0100
changeset 13 28aa5f597457
parent 12 27e41d7f5e8d
permissions -rw-r--r--
increment counters
franta-hg@12
     1
/**
franta-hg@12
     2
 * copy-image-resizer
franta-hg@12
     3
 * Copyright © 2014 František Kučera (frantovo.cz)
franta-hg@12
     4
 *
franta-hg@12
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@12
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@12
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@12
     8
 * (at your option) any later version.
franta-hg@12
     9
 *
franta-hg@12
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@12
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@12
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@12
    13
 * GNU General Public License for more details.
franta-hg@12
    14
 *
franta-hg@12
    15
 * You should have received a copy of the GNU General Public License
franta-hg@12
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@12
    17
 */
franta-hg@12
    18
package cz.frantovo.copyImageResizer;
franta-hg@12
    19
franta-hg@12
    20
import java.util.EnumMap;
franta-hg@12
    21
import java.util.Map;
franta-hg@12
    22
franta-hg@12
    23
/**
franta-hg@12
    24
 *
franta-hg@12
    25
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@12
    26
 */
franta-hg@12
    27
public class Counters {
franta-hg@12
    28
franta-hg@12
    29
	private final Map<COUNTER_TYPE, Integer> data = new EnumMap(COUNTER_TYPE.class);
franta-hg@12
    30
franta-hg@12
    31
	public int increment(COUNTER_TYPE counter) {
franta-hg@12
    32
		synchronized (data) {
franta-hg@12
    33
			int value = get(counter);
franta-hg@12
    34
			value++;
franta-hg@12
    35
			data.put(counter, value);
franta-hg@12
    36
			return value;
franta-hg@12
    37
		}
franta-hg@12
    38
	}
franta-hg@12
    39
franta-hg@12
    40
	public int get(COUNTER_TYPE counter) {
franta-hg@12
    41
		Integer value = data.get(counter);
franta-hg@12
    42
		return value == null ? 0 : value;
franta-hg@12
    43
	}
franta-hg@12
    44
franta-hg@12
    45
	@Override
franta-hg@12
    46
	public String toString() {
franta-hg@12
    47
		StringBuilder sb = new StringBuilder();
franta-hg@12
    48
franta-hg@12
    49
		sb.append("Counters: ");
franta-hg@12
    50
franta-hg@12
    51
		for (COUNTER_TYPE counter : COUNTER_TYPE.values()) {
franta-hg@12
    52
			sb.append(counter);
franta-hg@12
    53
			sb.append("=");
franta-hg@12
    54
			sb.append(get(counter));
franta-hg@12
    55
			sb.append(" ");
franta-hg@12
    56
			
franta-hg@12
    57
		}
franta-hg@12
    58
franta-hg@12
    59
		return sb.toString();
franta-hg@12
    60
	}
franta-hg@12
    61
franta-hg@12
    62
	public static enum COUNTER_TYPE {
franta-hg@12
    63
franta-hg@12
    64
		DIRECTORIES,
franta-hg@12
    65
		FILES,
franta-hg@12
    66
		RESIZED,
franta-hg@13
    67
		JUST_COPIED,
franta-hg@12
    68
		SKIPPED_UNKNOWN_EXTENSION,
franta-hg@12
    69
		SKIPPED_ERROR;
franta-hg@12
    70
franta-hg@12
    71
		@Override
franta-hg@12
    72
		public String toString() {
franta-hg@12
    73
			return name().toLowerCase();
franta-hg@12
    74
		}
franta-hg@12
    75
franta-hg@12
    76
	}
franta-hg@12
    77
}