<commit_before>// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package pipoint

import (
	"fmt"
	"reflect"
	"time"
)

// Param is a value or a struct that has age and validity.  Updating a
// Param also fires an event.
type Param struct {
	Name    string
	value   interface{}
	updated time.Time
	params  *Params
	final   bool
}

// Ok return true if the value has been recently updated.
func (p *Param) Ok() bool {
	if p.final {
		return true
	}
	elapsed := time.Now().Sub(p.updated)
	return elapsed.Seconds() < 3
}

// Get returns the current value which may be nil.
func (p *Param) Get() interface{} {
	return p.value
}

// GetFloat64 returns the current value as a float64.
func (p *Param) GetFloat64() float64 {
	return p.value.(float64)
}

// GetInt returns the current value as an int.
func (p *Param) GetInt() int {
	return int(p.GetFloat64())
}

func isNumber(value interface{}) bool {
	if value == nil {
		return false
	}

	switch value.(type) {
	case float64, int:
		return true
	default:
		return false
	}
}

// Set the value, update validity, and notify listeners.
func (p *Param) Set(value interface{}) error {
	if p.value == nil {
		// OK, nothing set yet.
	} else if isNumber(p.value) && isNumber(value) {
		// Number -> number is fine.
	} else if reflect.TypeOf(value) != reflect.TypeOf(p.value) {
		return fmt.Errorf("Type of %v changed from %v to %v",
			p.Name, p.value, value)
	}

	if isNumber(value) {
		// Store all numbers as float64.
		switch value.(type) {
		case float64:
			// No change
		case int:
			value = float64(value.(int))
		default:
			panic("Unsupported numeric type.")
		}
	}

	p.value = value
	p.updated = time.Now()
	p.final = false
	p.params.updated(p)
	return nil
}

// SetFloat64 tries to update the value as a float64.
func (p *Param) SetFloat64(value float64) error {
	return p.Set(value)
}

// SetInt tries to update the value as an int.
func (p *Param) SetInt(value int) error {
	return p.Set(value)
}

// Inc tries to increment the integer value.
func (p *Param) Inc() error {<commit_msg>pipoint: add param.Update() which sets if changed.<commit_after>// Copyright 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package pipoint

import (
	"fmt"
	"reflect"
	"time"
)

// Param is a value or a struct that has age and validity.  Updating a
// Param also fires an event.
type Param struct {
	Name    string
	value   interface{}
	updated time.Time
	params  *Params
	final   bool
}

// Ok return true if the value has been recently updated.
func (p *Param) Ok() bool {
	if p.final {
		return true
	}
	elapsed := time.Now().Sub(p.updated)
	return elapsed.Seconds() < 3
}

// Get returns the current value which may be nil.
func (p *Param) Get() interface{} {
	return p.value
}

// GetFloat64 returns the current value as a float64.
func (p *Param) GetFloat64() float64 {
	return p.value.(float64)
}

// GetInt returns the current value as an int.
func (p *Param) GetInt() int {
	return int(p.GetFloat64())
}

func isNumber(value interface{}) bool {
	if value == nil {
		return false
	}

	switch value.(type) {
	case float64, int:
		return true
	default:
		return false
	}
}

// Set the value, update validity, and notify listeners.
func (p *Param) Set(value interface{}) error {
	if p.value == nil {
		// OK, nothing set yet.
	} else if isNumber(p.value) && isNumber(value) {
		// Number -> number is fine.
	} else if reflect.TypeOf(value) != reflect.TypeOf(p.value) {
		return fmt.Errorf("Type of %v changed from %v to %v",
			p.Name, p.value, value)
	}

	if isNumber(value) {
		// Store all numbers as float64.
		switch value.(type) {
		case float64:
			// No change
		case int:
			value = float64(value.(int))
		default:
			panic("Unsupported numeric type.")
		}
	}

	p.value = value
	p.updated = time.Now()
	p.final = false
	p.params.updated(p)
	return nil
}

// SetFloat64 tries to update the value as a float64.
func (p *Param) SetFloat64(value float64) error {
	return p.Set(value)
}

// SetInt tries to update the value as an int.
func (p *Param) SetInt(value int) error {
	return p.Set(value)
}

// SetInt tries to update the value as an int.
func (p *Param) UpdateInt(value int) (bool, error) {
	if p.GetInt() == value {
		return false, nil
	}
	return true, p.Set(value)
}

// Inc tries to increment the integer value.
func (p *Param) Inc() error {
{'fraction_non_alphanumeric': 0.11681547619047619, 'fraction_numerical': 0.013764880952380952, 'mean_word_length': 3.177933177933178, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4948392', 'n_tokens_mistral': 1818, 'n_tokens_neox': 1727, 'n_words': 693}