Skip to content

Instantly share code, notes, and snippets.

@ryan-beckett
ryan-beckett / DirectoryServlet.java
Created January 20, 2012 23:33
A servlet to display the (public) files in your web root.
package com.service.file;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
@ryan-beckett
ryan-beckett / EmployeeClient.java
Created January 24, 2012 02:51
A quick demonstration of the MVC pattern.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class EmployeeClient {
@ryan-beckett
ryan-beckett / DataProviderTest.java
Created January 29, 2012 05:17
Demonstrates a frail implementation of the AbstractFactory pattern.
import java.util.*;
import java.lang.*;
class DataProviderTest
{
public static void main (String[] args) throws java.lang.Exception
{
String provider = "oracle"; //"derby"
DataProviderFactory factory = DataProviderFactory.getInstance();
DataProvider dp = factory.getProvider(provider);
@ryan-beckett
ryan-beckett / GraphSet.java
Created March 12, 2012 13:59
A generic directed graph that doesn't allow duplicate elements and performs topological sorting to detect cycles. All primitive operations are done in linear time. Sorting is done in polynomial time at most.
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
/**
*
* A generic directed graph that doesn't allow duplicate elements and performs
* topological sorting to detect cycles. All primitive operations are done in
@ryan-beckett
ryan-beckett / mips_ex1.asm
Created April 18, 2012 20:29
MIPS assembly: demonstrates the use of the stack for sending function arguments.
.text
main:
#populate arg registers
li $a0, 1
li $a1, 2
li $a2, 3
li $a3, 4
#push 5th arg on stack
li $t0, 10
@ryan-beckett
ryan-beckett / loop.mips
Created May 2, 2012 01:19
A quick looping test in MIPS
.text
main:
li $t4, 1, #t4 = counter
i1: ori $t0, $0, 1000
i2: ori $t1, $0, 2000
i3: addi $t2, $t0, 100
i4: #lw $t3, 0($t1)
i5: #lw $t4, 0($t0)
i6: add $t3, $t3, $t4
@ryan-beckett
ryan-beckett / LogicSolver.java
Created June 5, 2012 10:13
A quick and dirty implementation that parses and evaluates a logical expression in post-fix form.
import java.util.*;
import java.lang.*;
////////////////////////////////////////////////////////////////
// A quick and dirty implementation that parses and evaluates a
// logical expression in post-fix form. From left to right binary
// expressions are extracted (3 tokens), evaluated, and it's result
// is inserted into its place. This is done until the expression is
// gone. E.g.
//
@ryan-beckett
ryan-beckett / XMLValidator.java
Created October 4, 2012 22:53
Validates XML files against a specified schema or the default WC3 standard.
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
@ryan-beckett
ryan-beckett / JInterpreter.java
Created February 4, 2012 07:06
JInterpreterDemo: A demo displaying how the JavaCompiler API can be used to create an application that executes java code given as runtime input from a user. This gist also includes a facade for the JavaCompiler API and a JInterpreter class for interpreti
import java.io.*;
import java.net.*;
import java.util.*;
/**
* A Java statement interpreter. Add a list of statements with
* <code>newStatement</code> and run them with
* <code>run</code>. After running the interpreter, all
* previously added statements are forgotten.
*/
@ryan-beckett
ryan-beckett / CommentStripper.java
Created February 10, 2012 11:34
A denomstration of how to strip block-style and inline comments using a finite-state machine. Handles edge cases correctly. You can change the behavior of the class by overriding CommentStripper.doAction(int index).
import java.util.*;
public class CommentStripper {
private StaticTransitionTable transitions;
private StringBuilder sb;
private int index;
private int state;
private final char EOF = '@';