001package net.filebot.format;
002
003import static java.util.Arrays.*;
004
005import java.math.BigDecimal;
006import java.math.RoundingMode;
007import java.util.Iterator;
008
009// IMPORTANT NOTE:
010// Resolution must not be an instance of Collection
011// otherwise GString will use the default List representation for formatting
012// and not the toString() method from this class
013
014public class Resolution implements Iterable<Integer>, StringBinding {
015
016        private final int width;
017        private final int height;
018
019        public Resolution(int width, int height) {
020                this.width = width;
021                this.height = height;
022        }
023
024        public int getWidth() {
025                return width;
026        }
027
028        public int getHeight() {
029                return height;
030        }
031
032        public Number getMpx() {
033                return BigDecimal.valueOf(width * height / (double) 1e6).setScale(1, RoundingMode.HALF_UP);
034        }
035
036        @Override
037        public Iterator<Integer> iterator() {
038                return asList(width, height).iterator();
039        }
040
041        @Override
042        public String toString() {
043                return width + "x" + height;
044        }
045
046        public static Resolution parse(String width, String height) {
047                if (width == null || width.isEmpty() || height == null || height.isEmpty()) {
048                        return null;
049                }
050                return new Resolution(Integer.parseInt(width), Integer.parseInt(height));
051        }
052
053}