001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.video;
020
021import lucee.runtime.exp.PageException;
022import lucee.runtime.op.Caster;
023import lucee.runtime.op.Decision;
024
025public class Range {
026
027        public static final Range TRUE=new Range(0,-1);
028        public static final Range FALSE=new Range(0,0);
029        private double from;
030        private double to;
031
032        public Range(double from, double to) {
033                this.from=from;
034                this.to=to;
035        }
036
037
038        public static Range toRange(String def) throws PageException {
039                def=def.trim();
040                // boolean
041                if(Decision.isBoolean(def)) { 
042                        return Caster.toBooleanValue(def)?TRUE:FALSE;
043                }
044                
045                int index = def.indexOf(',');
046                // single value 
047                if(index==-1) {
048                        return new Range(toSeconds(def),-1);
049                }
050                
051                // double value
052                if(def.startsWith(","))def="0"+def;
053                if(def.endsWith(","))def+="-1";
054                
055                return new Range(toSeconds(def.substring(0,index)),toSeconds(def.substring(index+1)));
056                
057                
058        }
059        
060        private static double toSeconds(String str) throws PageException {
061                str=str.trim().toLowerCase();
062                
063                if(str.endsWith("ms"))  return Caster.toDoubleValue(str.substring(0,str.length()-2))/1000D;
064                else if(str.endsWith("s"))              return Caster.toDoubleValue(str.substring(0,str.length()-1));
065                else return Caster.toDoubleValue(str)/1000D;
066                // TODO if(str.endsWith("f"))                   this.startFrame=VideoConfig.toLong(str.substring(0,str.length()-1));
067                
068        }
069
070        /**
071         * @return the from
072         */
073        public double getFrom() {
074                return from;
075        }
076        
077        public String getFromAsString() {
078                return Caster.toString(from);
079        }
080
081
082        /**
083         * @return the to
084         */
085        public double getTo() {
086                return to;
087        }
088        
089        public String getToAsString() {
090                return Caster.toString(to);
091        }
092
093
094        /**
095         *
096         * @see java.lang.Object#equals(java.lang.Object)
097         */
098        public boolean equals(Object obj) {
099                if(obj==this) return true;
100                if(!(obj instanceof Range)) return false;
101                Range other=(Range) obj;
102                return other.from==from && other.to==to;
103        }
104
105        /**
106         *
107         * @see java.lang.Object#toString()
108         */
109        public String toString() {
110                return ""+from+":"+to+"";
111        }
112
113
114        public boolean show() {
115                return !equals(Range.FALSE);
116        }
117}