#  Draco Econometrics
#  Copyright (C) 2006-2008 Approximatrix, LLC
#  http://www.approximatrix.com
#  
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.

include Java
import com.approximatrix.draco.datamodel.VariableModel

module Model
  class Variable
    
    attr_reader :value
    
    def initialize(vmdl)
      @variable = vmdl
      @index = 0
      @value = 0
      set_value
    end
    
    def name
      @variable.getName()
    end
    
    def set_value
      @value = @variable.get(@index)
      @value
    end
    
    def increment
      update_index(@index+1)
    end
    
    def update_index(index)
      @index = index
      set_value
    end
    
    def each
      @variable.size().each do |i|
        val = nil
        begin
          val = @variable.get(i)
        rescue
          val = nil
        end
        yield val
      end
    end
    
    def to_a
      ret = []
      @variable.size().each do |i|
        begin
          ret << @variable.get(i)
        rescue
          ret << nil
        end
      end
      ret
    end
    
    def mean
      @variable.getMean()
    end
    
    def median
      @variable.getMedian()
    end
    
    def maximum
      @variable.getMaximumValue()
    end
    
    def minimum
      @variable.getMinimumValue()
    end
    
    def count
      @variable.getObservationCount()
    end
    
    def standard_deviation
      @variable.getColumnStandardDeviation()
    end

    def back(*args)
      args << 1 if args.length == 0 
      raise ArgumentError if args.length > 1
      begin
        @variable.get(@index-args[0])
      rescue
        nil
      end
    end
    
    def front(*args)
      args << 1 if args.length == 0 
      raise ArgumentError if args.length > 1
      begin
        @variable.get(@index+args[0])
      rescue
        nil
      end
    end
    
    def respond_to?(method_name)
      if self.methods.index(method_name)
        true
      else
        super or @value.respond_to? method_name
      end
    end
    
    def method_missing(m,*args)
      super unless @value.respond_to?(m)
      @value.send(m, *args) if @value.respond_to?(m)
    end
    
    alias stdev standard_deviation
    alias max maximum
    alias min minimum
    
  end # class Variable
end # module model
    
      
    
    
