Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Programmer > Re: To wrap or ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 16 Topic 52595 of 55513
Post > Topic >>

Re: To wrap or not to wrap?

by ram@[EMAIL PROTECTED] (Stefan Ram) May 8, 2008 at 10:54 PM

Aaron Fude <aaronfude@[EMAIL PROTECTED]
> writes:
>These functions catch exceptions and return null

  You must like the classic C idiom

if( f = fopen( "alpha", "r" ))
{ use( f );
  close( f ); }
else
{ handle_failure( "alpha", "r" ); }

  more than exception idioms like

try
{ f = fopen( "alpha", "r" );
  try
  { use( f ); }
  finally
  { close( f ); }}
catch( final Exception exception )
{ handleFailure( "alpha", "r", exception ); }

  I also like structured programing, and it is not easy
  for me to cope with the exception style. I hope that
  I at least have done the above translation correctly.

  The above code already looks complicated, but it becomes even
  more complicated, when you need to obtain and release
  /multiple/ resources. This should be done as a kind of
  transaction: When you need n resources, you might have
  obtained m already (m < n), but the next attempt might
  fail. Then you need to return to a orderly state and
  release exactly those resources that have been allocated
  so far and re****t the whole operation to be failed. So 
  the above patterns need to be nested, which makes the
  result even more complicated.

  I have invented another style, that use an object to handle
  control flow and should take care of the problem to allocate
  multiple resources

  Here is an example (explanation follows below):

public void digest( final java.io.File file )
{ final de.dclj.ram.system.program.Program program 
  = new de.dclj.ram.system.program.Program();
  try
  { 
    final java.io.FileInputStream fileInputStream = 
    program.'new java.io.FileInputStream'( file ); /* first attempt */

    final java.io.BufferedInputStream bufferedInputStream = 
    program.'new java.io.BufferedInputStream'( fileInputStream );

    final java.security.MessageDigest messageDigest =
    program.'new java.security.MessageDigest'( type );
    
    if( program.succeeded() )
    { this.process( bufferedInputStream, messageDigest ); }
    else
    { java.lang.System.out.println( program.'opening exceptions'() ); }}

  finally
  { program.close();
    if( !program.closed() )
    { java.lang.System.out.println( program.'closing exceptions'() ); }}}}

  A preprocessor converts everything withing single quotes to
  Java names. This is not necessary for this approach, but just
  an additional convenience.

  The operation »this.process« needs three resources: A file
  input stream, a buffered input stream and a message digest.

  The object »program« has operations to request the allocations
  needed. If the first attempt fails, it will skip the next two
  attempts and »program.succeeded()« will be false. The client
  above does not have to use »if« or »try« for each attempt.
  He can request the exceptions from the program object.

  »program.close()« will then close exactly those resources that
  have been obtained successfully before, because »program« has
  kept track of the release operations required to release
  everything that has been allocated successfully using this
  program object.

  Drawback: Each type of resource needs a special implementation
  in »de.dclj.ram.system.program.Program«, and right now there are
  only few types implemented. I will add additional code as I
  need it. See also:

http://www.purl.org/stefan_ram/html/ram.jar/de/dclj/ram/system/program/Program.html

  (with links to the source code.) This is part of the library

http://www.purl.org/stefan_ram/pub/ram-jar
 




 16 Posts in Topic:
To wrap or not to wrap?
Aaron Fude <aaronfude@  2008-05-08 15:19:42 
Re: To wrap or not to wrap?
ram@[EMAIL PROTECTED] (S  2008-05-08 22:54:08 
Re: To wrap or not to wrap?
=?ISO-8859-1?Q?Arne_Vajh=  2008-05-08 19:13:32 
Re: To wrap or not to wrap?
Aaron Fude <aaronfude@  2008-05-08 16:33:14 
Re: To wrap or not to wrap?
=?ISO-8859-1?Q?Arne_Vajh=  2008-05-08 19:36:50 
Re: To wrap or not to wrap?
ram@[EMAIL PROTECTED] (S  2008-05-08 23:44:34 
Re: To wrap or not to wrap?
"Chronic Philharmoni  2008-05-09 05:57:53 
Re: To wrap or not to wrap?
ram@[EMAIL PROTECTED] (S  2008-05-09 12:54:18 
Re: To wrap or not to wrap?
"Chronic Philharmoni  2008-05-10 18:33:24 
Re: To wrap or not to wrap?
"Daniel Dyer" &  2008-05-09 00:37:00 
Re: To wrap or not to wrap?
"Daniel Dyer" &  2008-05-09 00:42:54 
Re: To wrap or not to wrap?
Tom Anderson <twic@[EM  2008-05-09 00:37:43 
Re: To wrap or not to wrap?
Patricia Shanahan <pat  2008-05-08 20:46:58 
Re: To wrap or not to wrap?
Roedy Green <see_websi  2008-05-09 03:48:55 
Re: To wrap or not to wrap?
java_killer <ggl.book.  2008-05-09 00:48:08 
Re: To wrap or not to wrap?
Lew <lew@[EMAIL PROTEC  2008-05-09 09:17:21 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Dec 3 20:15:20 CST 2008.