packages feed

squeeze-1.0.2.0: src/Squeeze/Data/FileCombination.hs

{-
	Copyright (C) 2010 Dr. Alistair Ward

	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/>.
-}
{- |
 [@AUTHOR@]	Dr. Alistair Ward

 [@DESCRIPTION@]	A data-type which references a set of files by their paths, and qualifies them with their aggregate size.
-}

module Squeeze.Data.FileCombination (
-- * Types
-- ** Data-types
	FileCombination(
--		MkFileCombination,
		getAggregateFileSize
--		getFilePathList
	),
-- * Constants
	nullFileCombination,
-- * Functions
-- ** Constructors
	singleton,
-- ** Operators
	(<+>),
	(+>),
-- ** Predicates
	hasSize
) where

import qualified	Data.List
import qualified	Squeeze.Data.File	as Data.File

-- | Declare a list of files qualified by its aggregate size.
data FileCombination	= MkFileCombination {
	getAggregateFileSize	:: !Data.File.FileSize,		-- ^ The aggregate size of the files referenced by 'getFilePathList'.
	getFilePathList		:: Data.File.FilePathList	-- ^ A list of the paths, defining a set of files.
} deriving (Eq, Ord)

instance Show FileCombination where
	showsPrec _ MkFileCombination {
		getAggregateFileSize	= s,
		getFilePathList		= l
	} = shows s . showChar '\t' . shows (Data.List.sort l)

-- | A constant empty instance.
nullFileCombination :: FileCombination
nullFileCombination	= MkFileCombination 0 []

-- | Construct a 'FileCombination' from a single 'Data.File.FileSizeAndPath'.
singleton ::  Data.File.FileSizeAndPath -> FileCombination
singleton (fileSize, filePath)	= MkFileCombination fileSize [filePath]

-- | Prepend a 'Data.File.FileSizeAndPath' to an existing 'FileCombination'.
{-# INLINE (+>) #-}
(+>) ::
	Data.File.FileSizeAndPath	-- ^ The new path to prepend to the incumbent file-combination.
	-> FileCombination		-- ^ The incumbent combination of files.
	-> FileCombination
(fileSize, filePath) +> MkFileCombination {
	getAggregateFileSize	= s,
	getFilePathList		= l
} = MkFileCombination (fileSize + s) (filePath : l)

-- | Add two 'FileCombination's.
(<+>) ::
	FileCombination		-- ^ The combination to be prepended.
	-> FileCombination	-- ^ The incumbent to which the new value is prepended.
	-> FileCombination
MkFileCombination {
	getAggregateFileSize	= sL,
	getFilePathList		= lL
} <+> MkFileCombination {
	getAggregateFileSize	= sR,
	getFilePathList		= lR
} = MkFileCombination (sL + sR) (lL ++ lR)

-- | Predicate used to determine whether a specific file-combination matches a size-related requirement.
{-# INLINE hasSize #-}
hasSize ::
	(Data.File.FileSize -> Bool)	-- ^ The predicate.
	-> FileCombination		-- ^ The input datum to be tested.
	-> Bool
hasSize f MkFileCombination { getAggregateFileSize = s }	= f s