Skip to content

Commit

Permalink
#8 refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony4m committed Nov 22, 2024
1 parent ea6739c commit 3418b14
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions kfile/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ type BlockId struct {
}

func NewBlockId(filename string, blknum int) *BlockId {
if err := ValidateFilename(filename); err != nil {
panic(err)
}
if err := ValidateBlockNumber(blknum); err != nil {
panic(err)
}
return &BlockId{
Filename: filename,
Blknum: blknum,
Expand Down Expand Up @@ -50,3 +56,42 @@ func (b *BlockId) HashCode() uint32 {

return h.Sum32()
}

func (b *BlockId) Copy() *BlockId {
return NewBlockId(b.Filename, b.Blknum)
}

func (b *BlockId) NextBlock() *BlockId {
return NewBlockId(b.Filename, b.Blknum+1)
}

func (b *BlockId) PrevBlock() *BlockId {
if b.Blknum > 0 {
return NewBlockId(b.Filename, b.Blknum-1)
}
return nil
}

func (b *BlockId) IsFirst() bool {
return b.Blknum == 0
}

func ValidateBlockNumber(blknum int) error {
if blknum < 0 {
return fmt.Errorf("block number cannot be negative: %d", blknum)
}
return nil
}

func ValidateFilename(filename string) error {
if filename == "" {
return fmt.Errorf("filename cannot be empty")
}
return nil
}

func (b *BlockId) IsValid() bool {
return b != nil &&
b.Filename != "" &&
b.Blknum >= 0
}

0 comments on commit 3418b14

Please sign in to comment.