github.com/mattdotmatt/gauge@v0.3.2-0.20160421115137-425a4cdccb62/gauge/specCollection.go (about)

     1  // Copyright 2015 ThoughtWorks, Inc.
     2  
     3  // This file is part of Gauge.
     4  
     5  // Gauge is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  
    10  // Gauge is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  
    15  // You should have received a copy of the GNU General Public License
    16  // along with Gauge.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package gauge
    19  
    20  import "sync"
    21  
    22  type SpecCollection struct {
    23  	mutex sync.Mutex
    24  	index int
    25  	specs []*Specification
    26  }
    27  
    28  func NewSpecCollection(s []*Specification) *SpecCollection {
    29  	return &SpecCollection{specs: s}
    30  }
    31  
    32  func (s *SpecCollection) Add(spec *Specification) {
    33  	s.specs = append(s.specs, spec)
    34  }
    35  
    36  func (s *SpecCollection) Specs() []*Specification {
    37  	return s.specs
    38  }
    39  
    40  func (s *SpecCollection) HasNext() bool {
    41  	s.mutex.Lock()
    42  	defer s.mutex.Unlock()
    43  	return s.index < len(s.specs)
    44  }
    45  
    46  func (s *SpecCollection) Next() *Specification {
    47  	s.mutex.Lock()
    48  	defer s.mutex.Unlock()
    49  	spec := s.specs[s.index]
    50  	s.index++
    51  	return spec
    52  }
    53  
    54  func (s *SpecCollection) Size() int {
    55  	s.mutex.Lock()
    56  	defer s.mutex.Unlock()
    57  	length := len(s.specs)
    58  	return length
    59  }
    60  
    61  func (s *SpecCollection) SpecNames() []string {
    62  	specNames := make([]string, 0)
    63  	for _, spec := range s.specs {
    64  		specNames = append(specNames, spec.FileName)
    65  	}
    66  	return specNames
    67  }