forked from n10v/id3v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sequence.go
63 lines (52 loc) · 1.34 KB
/
sequence.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright 2016 Albert Nigmatzianov. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package id3v2
import (
"sync"
)
// sequence is used to manipulate with frames, which can be in tag
// more than one (e.g. APIC, COMM, USLT and etc.)
type sequence struct {
framers map[string]Framer
framesCache []Framer
}
func (s *sequence) AddFrame(f Framer) {
s.framesCache = s.framesCache[:0]
var id string
if cf, ok := f.(CommentFrame); ok {
id = cf.Language + cf.Description
} else if pf, ok := f.(PictureFrame); ok {
id = pf.Description
} else if uslf, ok := f.(UnsynchronisedLyricsFrame); ok {
id = uslf.Language + uslf.ContentDescriptor
} else {
panic("sequence: unknown type of Framer")
}
s.framers[id] = f
}
func (s *sequence) Count() int {
return len(s.framers)
}
func (s *sequence) Frames() []Framer {
if len(s.framesCache) == 0 {
for _, f := range s.framers {
s.framesCache = append(s.framesCache, f)
}
}
return s.framesCache
}
var seqPool = sync.Pool{New: func() interface{} {
return &sequence{framers: make(map[string]Framer)}
}}
func getSequence() *sequence {
s := seqPool.Get().(*sequence)
if s.Count() > 0 {
s.framers = make(map[string]Framer)
s.framesCache = s.framesCache[:0]
}
return s
}
func putSequence(s *sequence) {
seqPool.Put(s)
}