java-bridge-extras (empty) → 0.9
raw patch · 6 files changed
+217/−0 lines, 6 filesdep +basedep +java-bridgedep +transformerssetup-changed
Dependencies added: base, java-bridge, transformers
Files
- LICENSE +18/−0
- Setup.hs +4/−0
- java-bridge-extras.cabal +33/−0
- src/Foreign/Java/Control.hs +51/−0
- src/Foreign/Java/IO.hs +44/−0
- src/Foreign/Java/Maybe.hs +67/−0
+ LICENSE view
@@ -0,0 +1,18 @@+Permission is hereby granted, free of charge, to any person obtaining+a copy of this software and associated documentation files (the "Software"),+to deal in the Software without restriction, including without limitation+the rights to use, copy, modify, merge, publish, distribute, sublicense,+and/or sell copies of the Software, and to permit persons to whom the+Software is furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included+in all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL+THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER+DEALINGS IN THE SOFTWARE.+
+ Setup.hs view
@@ -0,0 +1,4 @@+import Distribution.Simple++main = defaultMain+
+ java-bridge-extras.cabal view
@@ -0,0 +1,33 @@+name: java-bridge-extras+version: 0.9++license: MIT+license-file: LICENSE++author: Julian Fleischer <julian.fleischer@fu-berlin.de>+maintainer: Julian Fleischer <julian.fleischer@fu-berlin.de>++stability: experimental+category: Foreign, Java, JVM, FFI Tools++cabal-version: >= 1.8++synopsis: Utilities for working with the java-bridge package.+description: Utilities for working with the java-bridge package.++build-type: Simple+++Library+ build-depends: base >= 4.5 && < 5+ , java-bridge == 0.9+ , transformers >= 0.3+++ hs-source-dirs: src++ exposed-modules: Foreign.Java.Control+ , Foreign.Java.IO+ , Foreign.Java.Maybe++
+ src/Foreign/Java/Control.hs view
@@ -0,0 +1,51 @@+{-# LANGUAGE Haskell2010 #-}+{-# OPTIONS -Wall #-}++-- | Utilities for controlling actions inside the Java monad.+module Foreign.Java.Control where++import Prelude hiding (pred, until)++when :: Monad m => m Bool -> m () -> m ()+-- ^ Execute an action if the given predicate+-- evaluates to 'True'.+when pred action = pred >>= (\p -> if p then action else return ())++unless :: Monad m => m Bool -> m () -> m ()+-- ^ Execute an action if the given predicate+-- evaluates to 'False'.+unless pred action = pred >>= (\p -> if not p then action else return ())++whether :: Monad m => m Bool -> m a -> m a -> m a+-- ^ Execute either the first or the second action,+-- depending on whether the given predicate evaluates+-- to 'True' or 'False'.+whether pred actionIf actionElse = do+ cond <- pred+ if cond then actionIf else actionElse++while :: Monad m => m Bool -> m () -> m ()+-- ^ Run a computation as long as the given predicate+-- evaluates to 'True'.+while pred action = do+ cond <- pred+ if cond then action >> while pred action+ else return ()++for :: Monad m => a -> (a -> m Bool) -> (a -> m a) -> m a+-- ^ Reiterate a computation on a given value as long+-- as a condition is 'True'.+for i pred action = do+ continue <- pred i+ if continue then action i >>= (\i' -> for i' pred action)+ else return i++until :: Monad m => a -> (a -> m (Bool, a)) -> m a+-- ^ Reiterate a computation on a given value until+-- a condition is 'True'.+until i action = do+ (done, result) <- action i+ if done then return result+ else until result action++
+ src/Foreign/Java/IO.hs view
@@ -0,0 +1,44 @@+{-# LANGUAGE Haskell2010+ , TypeSynonymInstances+ , FlexibleInstances+ , UndecidableInstances+ , OverlappingInstances+ #-}+{-# OPTIONS -Wall #-}++-- | +-- Module : Foreign.Java.IO+-- Copyright : (c) Julian Fleischer 2013+-- License : MIT (See LICENSE file in cabal package)+--+-- Maintainer : julian.fleischer@fu-berlin.de+-- Stability : provisional+-- Portability : non-portable (UndecidableInstances, OverlappingInstances)+--+-- Utilities to ease IO operations in the Java monad.+module Foreign.Java.IO where++import Prelude hiding (print)+import Control.Monad.IO.Class+import System.IO hiding (print)+++class PrintLn a where+ -- | Like @'putStrLn' . 'show'@, but with a specialized+ -- version for Strings plus it can be used within any+ -- 'MonadIO' monad (such as IO and Java).+ println :: MonadIO m => a -> m ()++ -- | Like @'putStr' . 'show'@, but with a specialized+ -- version for Strings plus it can be used within any+ -- 'MonadIO' monad (such as IO and Java).+ print :: MonadIO m => a -> m ()++instance PrintLn String where+ println = liftIO . putStrLn+ print x = liftIO (putStr x >> hFlush stdout)++instance Show a => PrintLn a where+ println = println . show+ print = print . show+
+ src/Foreign/Java/Maybe.hs view
@@ -0,0 +1,67 @@+{-# LANGUAGE Haskell2010 #-}+{-# OPTIONS+ -Wall+ -fno-warn-orphans+ #-}++-- |+-- Module : Foreign.Java.Maybe+-- Copyright : (c) Julian Fleischer 2013+-- License : MIT (See LICENSE file in cabal package)+--+-- Maintainer : julian.fleischer@fu-berlin.de+-- Stability : provisional+-- Portability : portable (Haskell2010)+--+-- Every java methods returns in priniciple either Nothing or+-- Just a value. This is quite cumbersome to work with. This module+-- contains utility functions for working with Maybe values in the+-- java monad.+--+-- This module offers the orphan instance (!)+--+-- > instance JavaObject a => JavaObject (Maybe a)+--+-- This instance allows you to apply 'toString' and the like+-- without unwrapping a 'Maybe' value.+--+-- Note that the 'asObject' function provided by this instance+-- is undefined (since @null@ is not an object). This is also the+-- reason why this instance is not included in "Foreign.Java" by+-- default. Invoking it will call @error "NullPointerException"@.+-- In other words: This fine module will bring back all the joy of+-- Java you might miss in Haskell :-)+--+-- 'toString' will return @null@ for @Nothing@.+--+-- 'hashCode' will return @0@ for @Nothing@.+--+-- 'classOf' will return the class for @java.lang.Void@ on @Nothing@,+-- as this is a class for which there gare no object instances. This is+-- only a stopgap and slightly incorrect, as null is not an object,+-- and does therefor not have a class.+module Foreign.Java.Maybe where+++import Foreign.Java+++instance JavaObject a => JavaObject (Maybe a) where++ toString m = case m of+ Just obj -> toString obj+ Nothing -> return "null"++ hashCode m = case m of+ Just obj -> hashCode obj+ Nothing -> return 0++ asObject m = case m of+ Just obj -> asObject obj+ Nothing -> error "NullPointerException"++ classOf m = case m of+ Just obj -> classOf obj+ Nothing -> getClass "java.lang.Void" >>= classOf++