Skip to content
This repository has been archived by the owner on Apr 30, 2019. It is now read-only.

Modify Otto project in order to support J2Objc. #173

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
<!--module name="DesignForExtension"/-->
<module name="FinalClass"/>
<module name="HideUtilityClassConstructor"/>
<module name="InterfaceIsType"/>
<!-- module name="InterfaceIsType"/-->
<!--module name="VisibilityModifier"/-->


Expand Down
71 changes: 71 additions & 0 deletions otto-android/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2012 Square, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.squareup</groupId>
<artifactId>otto-parent</artifactId>
<version>1.3.9-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<groupId>com.squareup</groupId>
<artifactId>otto-android</artifactId>
<packaging>jar</packaging>
<name>Otto Android</name>

<dependencies>
<dependency>
<groupId>com.squareup</groupId>
<artifactId>otto</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easytesting</groupId>
<artifactId>fest-assert-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>${binary.prefix}-${project.artifactId}-${project.version}</finalName>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2012 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.squareup.otto;

import android.os.Looper;

/**
* Enforces a thread confinement policy for methods on a particular event bus.
*
* This class contains Android specific Thread enforcers.
*
* @author Jake Wharton
*/
public interface ThreadEnforcerAndroid extends ThreadEnforcer {

/** A {@link ThreadEnforcer} that confines {@link Bus} methods to the main thread. */
ThreadEnforcer MAIN = new ThreadEnforcer() {
@Override public void enforce(Bus bus) {
if (Looper.myLooper() != Looper.getMainLooper()) {
throw new IllegalStateException("Event bus " + bus + " accessed from non-main thread " + Looper.myLooper());
}
}
};

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2012 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.squareup.otto;

import org.junit.Test;

import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;

public class ThreadEnforcerTest {

private static class RecordingThreadEnforcer implements ThreadEnforcer {
boolean called = false;

@Override public void enforce(Bus bus) {
called = true;
}
}

@Test public void enforerCalledForRegister() {
RecordingThreadEnforcer enforcer = new RecordingThreadEnforcer();
Bus bus = new Bus(enforcer);

assertFalse(enforcer.called);
bus.register(this);
assertTrue(enforcer.called);
}

@Test public void enforcerCalledForPost() {
RecordingThreadEnforcer enforcer = new RecordingThreadEnforcer();
Bus bus = new Bus(enforcer);

assertFalse(enforcer.called);
bus.post(this);
assertTrue(enforcer.called);
}

@Test public void enforcerCalledForUnregister() {
RecordingThreadEnforcer enforcer = new RecordingThreadEnforcer();
Bus bus = new Bus(enforcer);

assertFalse(enforcer.called);
bus.unregister(this);
assertTrue(enforcer.called);
}

}
5 changes: 5 additions & 0 deletions otto-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
<artifactId>otto</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.squareup</groupId>
<artifactId>otto-android</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
package com.squareup.otto.sample;

import com.squareup.otto.Bus;
import com.squareup.otto.ThreadEnforcerAndroid;

/**
* Maintains a singleton instance for obtaining the bus. Ideally this would be replaced with a more efficient means
* such as through injection directly into interested classes.
*/
public final class BusProvider {
private static final Bus BUS = new Bus();
private static final Bus BUS = new Bus(ThreadEnforcerAndroid.MAIN);

public static Bus getInstance() {
return BUS;
Expand Down
6 changes: 0 additions & 6 deletions otto/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@
<name>Otto</name>

<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
8 changes: 4 additions & 4 deletions otto/src/main/java/com/squareup/otto/Bus.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
* <p>Handlers should not, in general, throw. If they do, the Bus will wrap the exception and
* re-throw it.
*
* <p>The Bus by default enforces that all interactions occur on the main thread. You can provide an alternate
* <p>The Bus does not by default enforces any thread. You can provide an alternate
* enforcement by passing a {@link ThreadEnforcer} to the constructor.
*
* <h2>Producer Methods</h2>
Expand Down Expand Up @@ -119,18 +119,18 @@ public class Bus {
}
};

/** Creates a new Bus named "default" that enforces actions on the main thread. */
/** Creates a new Bus named "default" that does not enforce actions on a given thread. */
public Bus() {
this(DEFAULT_IDENTIFIER);
}

/**
* Creates a new Bus with the given {@code identifier} that enforces actions on the main thread.
* Creates a new Bus with the given {@code identifier} that does not enforce actions on a given thread.
*
* @param identifier a brief name for this bus, for debugging purposes. Should be a valid Java identifier.
*/
public Bus(String identifier) {
this(ThreadEnforcer.MAIN, identifier);
this(ThreadEnforcer.ANY, identifier);
}

/**
Expand Down
13 changes: 0 additions & 13 deletions otto/src/main/java/com/squareup/otto/ThreadEnforcer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package com.squareup.otto;

import android.os.Looper;

/**
* Enforces a thread confinement policy for methods on a particular event bus.
*
Expand All @@ -32,21 +30,10 @@ public interface ThreadEnforcer {
*/
void enforce(Bus bus);


/** A {@link ThreadEnforcer} that does no verification. */
ThreadEnforcer ANY = new ThreadEnforcer() {
@Override public void enforce(Bus bus) {
// Allow any thread.
}
};

/** A {@link ThreadEnforcer} that confines {@link Bus} methods to the main thread. */
ThreadEnforcer MAIN = new ThreadEnforcer() {
@Override public void enforce(Bus bus) {
if (Looper.myLooper() != Looper.getMainLooper()) {
throw new IllegalStateException("Event bus " + bus + " accessed from non-main thread " + Looper.myLooper());
}
}
};

}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

<modules>
<module>otto</module>
<module>otto-android</module>
<module>otto-sample</module>
</modules>

Expand Down
15 changes: 12 additions & 3 deletions website/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,14 @@ <h4>Producing</h4>

<h4>Thread Enforcement</h4>
<p>Since at times it may be ambiguous on which thread you are receiving callbacks, Otto provides an
enforcement mechanism to ensure you are always called on the thread you desire. By default, all interaction
with an instance is confined to the main thread.</p>
enforcement mechanism to ensure you are always called on the thread you desire. By default, no enforcement
is performed.</p>
<pre class="prettyprint">// Both of these are functionally equivalent.
Bus bus1 = new Bus();
Bus bus2 = new Bus(ThreadEnforcer.MAIN);</pre>
Bus bus2 = new Bus(ThreadEnforcer.ANY);</pre>
<pre class="prettyprint">// To enforce Android Main Thread use the following.
Bus bus2 = new Bus(ThreadEnforcerAndroid.MAIN);</pre>

<p>If you are not concerned on which thread interaction is occurring, instantiate a bus instance with
<code>ThreadEnforcer.ANY</code>. You can also provide your own implementation of the
<code>ThreadEnforcer</code> interface if you need additional functionality or validation.</p>
Expand All @@ -121,13 +124,19 @@ <h3 id="download">Download</h3>
<h4>Gradle</h4>
<pre class="prettyprint">dependencies {
compile 'com.squareup:otto:<span class="version">+</span>'
compile 'com.squareup:otto-android:<span class="version">+</span>'
}</pre>

<h4>Maven</h4>
<pre class="prettyprint">&lt;dependency>
&lt;groupId>com.squareup&lt;/groupId>
&lt;artifactId>otto&lt;/artifactId>
&lt;version><span class="version pln"><em>(insert latest version)</em></span>&lt;/version>
&lt;/dependency>
&lt;dependency>
&lt;groupId>com.squareup&lt;/groupId>
&lt;artifactId>otto-android&lt;/artifactId>
&lt;version><span class="version pln"><em>(insert latest version)</em></span>&lt;/version>
&lt;/dependency></pre>
<p>Once you've installed Otto, add the following lines to your <code>proguard-project.txt</code> file:</p>
<pre class="prettyprint">-keepattributes *Annotation*
Expand Down