packages feed

squeeze-1.0.1.4: src/Squeeze/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 type-synonym for a combination of files, and associated operations.
-}

module Squeeze.FileCombination (
-- * Types
-- ** Type-synonyms
	FileCombination,
-- * Constants
	nullFileCombination,
-- * Functions
	concatenate,
	hasSize,
	mkCombination,
	prepend,
	singleton,
	showFileCombination
) where

import			Control.Arrow((***), (&&&))
import qualified	Control.Arrow
import qualified	Data.List
import qualified	Squeeze.File	as File

-- | Declare a list of files qualified by its aggregate size.
type FileCombination	= (File.FileSize, File.FilePathList)

-- | A constant.
nullFileCombination :: FileCombination
nullFileCombination	= (0, [])

-- | Create a 'FileCombination' from a list of 'File.FileSizeAndPath'.
mkCombination :: [File.FileSizeAndPath] -> FileCombination
mkCombination	= File.aggregateSize &&& map File.getPath

-- | Construct a 'FileCombination' from a single 'File.FileSizeAndPath'.
singleton ::  File.FileSizeAndPath -> FileCombination
singleton	= Control.Arrow.second return

-- | Prepend a 'File.FileSizeAndPath' to an existing 'FileCombination'.
prepend ::
	File.FileSizeAndPath	-- ^ The new path to prepend to the existing file-combination.
	-> FileCombination	-- ^ The incumbent combination of files.
	-> FileCombination
prepend (fileSize, filePath)	= (fileSize +) *** (filePath :)

-- | Concatenate two 'FileCombination's.
concatenate :: FileCombination -> FileCombination -> FileCombination
concatenate (fileSize, filePathList)	= (fileSize +) *** (filePathList ++)

-- | Output.
showFileCombination :: FileCombination -> ShowS
showFileCombination (fileSize, filePathList)	= shows fileSize . showChar '\t' . shows (Data.List.sort filePathList)

-- | Predicate used to determine whether a specific file-combination matches a size-related requirement.
hasSize :: (File.FileSize -> Bool) -> FileCombination -> Bool
hasSize f	= f . fst