TestFactorial.java: factorial computation in Java

Generator QR Code 2d barcode in Java TestFactorial.java: factorial computation

Listing 3.4 TestFactorial.java: factorial computation
QR Code 2d Barcode Encoder In Java
Using Barcode generator for Java Control to generate, create QR-Code image in Java applications.
www.OnBarcode.com
Scan Denso QR Bar Code In Java
Using Barcode scanner for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
import java.util.*; public class TestFactorial { public static void main(String[] args) { System.out.println("Result: " + factorial(5) + "\n"); System.out.println("Result: " + factorial(10) + "\n"); System.out.println("Result: " + factorial(15) + "\n");
Barcode Maker In Java
Using Barcode maker for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
UPC A Creation In Java
Using Barcode creation for Java Control to generate, create Universal Product Code version A image in Java applications.
www.OnBarcode.com
Advice
Barcode Generation In Java
Using Barcode creator for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
GS1-128 Maker In Java
Using Barcode drawer for Java Control to generate, create EAN / UCC - 14 image in Java applications.
www.OnBarcode.com
System.out.println("Result: " + factorial(15) + "\n"); } public static long factorial(int n) { if (n == 0) { return 1; } else { return n * factorial(n-1); } } }
Linear Barcode Printer In Java
Using Barcode drawer for Java Control to generate, create Linear Barcode image in Java applications.
www.OnBarcode.com
Paint Postnet 3 Of 5 In Java
Using Barcode generator for Java Control to generate, create Delivery Point Barcode (DPBC) image in Java applications.
www.OnBarcode.com
Now let s write the aspect to optimize the factorial computation by caching the computed value for later use, as shown in listing 3.5.
QR Code ISO/IEC18004 Printer In Java
Using Barcode generator for Java Control to generate, create Denso QR Bar Code image in Java applications.
www.OnBarcode.com
QR Code ISO/IEC18004 Creator In Objective-C
Using Barcode creator for iPad Control to generate, create QR image in iPad applications.
www.OnBarcode.com
Listing 3.5 OptimizeFactorialAspect.java: aspect for caching results
Barcode Drawer In .NET
Using Barcode creator for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
ECC200 Generation In Java
Using Barcode drawer for Android Control to generate, create Data Matrix 2d barcode image in Android applications.
www.OnBarcode.com
import java.util.*; public aspect OptimizeFactorialAspect { pointcut factorialOperation(int n) : call(long *.factorial(int)) && args(n); pointcut topLevelFactorialOperation(int n) : factorialOperation(n) && !cflowbelow(factorialOperation(int)); private Map _factorialCache = new HashMap();
EAN13 Drawer In Visual C#.NET
Using Barcode drawer for .NET Control to generate, create GTIN - 13 image in .NET applications.
www.OnBarcode.com
GTIN - 12 Generator In .NET
Using Barcode creator for Reporting Service Control to generate, create GS1 - 12 image in Reporting Service applications.
www.OnBarcode.com
Capturing context using args() Capturing context from another pointcut
UCC - 12 Drawer In None
Using Barcode creator for Font Control to generate, create UPC Symbol image in Font applications.
www.OnBarcode.com
Print Barcode In Java
Using Barcode drawer for BIRT Control to generate, create Barcode image in BIRT reports applications.
www.OnBarcode.com
Using pointcut s context
UPC - 13 Drawer In VB.NET
Using Barcode printer for .NET framework Control to generate, create EAN13 image in VS .NET applications.
www.OnBarcode.com
Read Code 39 Full ASCII In .NET
Using Barcode recognizer for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
before(int n) : topLevelFactorialOperation(n) { System.out.println("Seeking factorial for " + n); } Returning primitive long around(int n) : factorialOperation(n) { Object cachedValue = _factorialCache.get(new Integer(n)); if (cachedValue != null) { System.out.println("Found cached value for " + n + ": " + cachedValue); return ((Long)cachedValue).longValue(); } Passing along context return proceed(n); to proceed() }
Encode Matrix 2D Barcode In .NET
Using Barcode generation for ASP.NET Control to generate, create 2D Barcode image in ASP.NET applications.
www.OnBarcode.com
Print QR Code ISO/IEC18004 In Objective-C
Using Barcode generation for iPhone Control to generate, create QR Code 2d barcode image in iPhone applications.
www.OnBarcode.com
from around advice
Capturing after(int n) returning(long result) return value : topLevelFactorialOperation(n) { _factorialCache.put(new Integer(n), new Long(result)); }
AspectJ: syntax basics
d e f g
The factorialOperation() pointcut captures all calls to the factorial() method. It also collects the argument to the method. The topLevelFactorialOperation() pointcut captures all nonrecursive calls to the factorial() method. It captures the context available in any factorialOperation() pointcut it uses. See figure 3.5 for a graphical representation of capturing context using named pointcuts. The before advice logs the nonrecursive factorial() method invocation. In the log message, it uses the collected context. The around advice to any factorial() method invocation also uses the context. It declares that it will return a long matching the return type of the advised join point. The around advice passes the captured context to proceed(). Recall that the number and type of arguments to proceed() must match the advice itself. The after returning advice collects the return value by specifying its type and identifier in the returning() part. It then uses the return value as well as the context collected from the join point to update the cache. When we compile and run the code, we get the following output:
> ajc TestFactorial.java OptimizeFactorialAspect.java > java TestFactorial Seeking factorial for 5 Result: 120 Seeking factorial for 10 Found cached value for 5: 120 Result: 3628800 Seeking factorial for 15 Found cached value for 10: 3628800 Result: 1307674368000 Seeking factorial for 15 Found cached value for 15: 1307674368000 Result: 1307674368000
As soon as a cached value is found, the factorial computation uses that value instead of continuing with the recursive computation. For example, while computing a factorial for 15, the computation uses a pre-cached factorial value for 10.
NOTE
It seems that you could simply modify the Test.factorial() method to insert code for caching optimization, especially since only one method needs to be modified. However, such an implementation will tangle the optimization logic with factorial computation logic. With conventional
Static crosscutting
refactoring techniques, you can limit the inserted code to a few lines. Using an aspect, you refactor the caching completely out of the core factorial computation code. You can now modify the caching strategy without even touching the factorial() method.
3.3 Static crosscutting
In AOP, we often find that in addition to affecting dynamic behavior using advice, it is necessary for aspects to affect the static structure in a crosscutting manner. While dynamic crosscutting modifies the execution behavior of the program, static crosscutting modifies the static structure of the types the classes, interfaces, and other aspects and their compile-time behavior. There are four broad classifications of static crosscutting: member introduction, type-hierarchy modification, compile-time error and warning declaration, and exception softening. In this section, we study the first three kinds. Understanding exception softening requires additional design considerations for effective use, and we will visit that along with other similar topics in chapter 4. 3.3.1 Member introduction Aspects often need to introduce data members and methods into the aspected classes. For example, in a banking system, implementing a minimum balance rule may require additional data members corresponding to a minimum balance and a method for computing the available balance. AspectJ provides a mechanism called introduction to introduce such members into the specified classes and interfaces in a crosscutting manner. The code snippet in listing 3.6 introduces the _minimumBalance field and the getAvailableBalance() method to the Account class. The after advice sets the minimum balance in SavingsAccount to 25.
Copyright © OnBarcode.com . All rights reserved.