Example usage for java.lang.ref WeakReference WeakReference

List of usage examples for java.lang.ref WeakReference WeakReference

Introduction

In this page you can find the example usage for java.lang.ref WeakReference WeakReference.

Prototype

public WeakReference(T referent) 

Source Link

Document

Creates a new weak reference that refers to the given object.

Usage

From source file:hudson.plugins.robot.RobotBuildAction.java

/**
 * Returns Robotresult. If not in memory loads it from disk.
 *///from w w w .  j  a  v a2  s. c  o  m
public synchronized RobotResult getResult() {
    RobotResult returnable;
    if (result != null)
        return result;
    if (resultReference == null) {
        returnable = load();
        resultReference = new WeakReference<RobotResult>(returnable);
    } else {
        returnable = resultReference.get();
    }

    if (returnable == null) {
        returnable = load();
        resultReference = new WeakReference<RobotResult>(returnable);
    }
    return returnable;
}

From source file:com.actionbarsherlock.internal.view.MenuItemImpl.java

public void setItemView(int type, MenuView.ItemView itemView) {
    mItemViews[type] = new WeakReference<MenuView.ItemView>(itemView);
}

From source file:com.bullmobi.base.content.ConfigBase.java

/**
 * Adds new {@link java.lang.ref.WeakReference weak} listener to the config. Make sure you call
 * {@link #unregisterListener(ConfigBase.OnConfigChangedListener)} later!
 *
 * @param listener a listener to register to config changes.
 * @see #unregisterListener(ConfigBase.OnConfigChangedListener)
 *//*from w  w  w  .j av  a  2s. co  m*/
@Override
public final void registerListener(@NonNull OnConfigChangedListener listener) {
    // Make sure to register listener only once.
    for (WeakReference<OnConfigChangedListener> ref : mListenersRefs) {
        if (ref.get() == listener) {
            Log.w(TAG, "Tried to register already registered listener!");
            return;
        }
    }

    mListenersRefs.add(new WeakReference<>(listener));
}

From source file:com.achep.base.content.ConfigBase.java

/**
 * Adds new {@link java.lang.ref.WeakReference weak} listener to the config. Make sure you call
 * {@link #unregisterListener(ConfigBase.OnConfigChangedListener)} later!
 *
 * @param listener a listener to register to config changes.
 * @see #unregisterListener(ConfigBase.OnConfigChangedListener)
 *//*from w  w  w.j  av a  2s .co  m*/
@Override
public final void registerListener(@NonNull OnConfigChangedListener listener) {
    // Make sure to register listener only once.
    for (WeakReference<OnConfigChangedListener> ref : mListenersRefs) {
        if (ref.get() == listener) {
            Timber.tag(TAG).w("Tried to register already registered listener!");
            return;
        }
    }

    mListenersRefs.add(new WeakReference<>(listener));
}

From source file:br.com.anteros.social.linkedin.AnterosLinkedInSession.java

@Override
public void setActivity(Activity activity) {
    this.activity = new WeakReference<Activity>(activity);
}

From source file:ch.entwine.weblounge.common.impl.content.AbstractResourceReaderImpl.java

/**
 * This method is called when a <code>Page</code> object is instantiated.
 * /*from   www . j a  va 2s  . c  om*/
 * @param is
 *          the xml input stream
 * @param uri
 *          the page uri
 * 
 * @throws IOException
 *           if reading the input stream fails
 */
public T read(InputStream is, Site site) throws SAXException, IOException, ParserConfigurationException {
    reset();
    resource = createResource(site);
    readHeader = true;
    readBody = true;
    SAXParser parser = parserRef.get();
    if (parser == null) {
        parser = parserFactory.newSAXParser();
        parserRef = new WeakReference<SAXParser>(parser);
    }
    parser.parse(is, this);
    return resource;
}

From source file:org.fastmongo.odm.dbobject.mapping.core.DbObjectToDomainConverterTest.java

@Test
public void testGarbageCollectedRootObjects() {
    DBObject db;//from   ww  w. ja  v  a2s  .co  m
    {
        Group root = TestDomainObjectsFixtures.createGroup(5, 10);
        db = toDbConverter.toDbObject(root);
    }

    Group root = fromDbConverter.fromDbObject(db, Group.class);
    Reference<Group> ref = new WeakReference<>(root);
    List<User> list = root.getUsers();

    Assert.assertNotNull(ref.get());

    //noinspection UnusedAssignment
    root = null;

    Runtime.getRuntime().gc();

    int limit = 10;
    int wait = 0;
    while (ref.get() != null && wait++ < limit) {
        if (ref.get() == null) {
            break;
        }

        LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(100)); // wait a bit
    }

    Assert.assertNull(ref.get());

    // touch proxy, it should not throw NPE as before
    Assert.assertTrue(!list.isEmpty());
}

From source file:com.frostwire.android.gui.adapters.TransferListAdapter.java

public TransferListAdapter(Context context, List<Transfer> list) {
    this.contextRef = new WeakReference<>(context);
    this.dialogs = new ArrayList<>();
    this.list = list.equals(Collections.emptyList()) ? new ArrayList<>() : list;
    viewOnClickListener = new ViewOnClickListener();
    viewOnLongClickListener = new ViewOnLongClickListener();
    openOnClickListener = new OpenOnClickListener(context);
    transferDetailsClickListener = new TransferDetailsClickListener(context);
}

From source file:ch.entwine.weblounge.common.impl.content.page.PageReader.java

/**
 * This method is called when a <code>Page</code> object is instantiated.
 * /*w w w .j a va2  s.  c  om*/
 * @param is
 *          the xml input stream
 * @param site
 *          the page's site
 * 
 * @throws IOException
 *           if reading the input stream fails
 */
public PageImpl read(InputStream is, Site site) throws SAXException, IOException, ParserConfigurationException {
    reset();
    page = new PageImpl(new PageURIImpl(site, "/"));
    readHeader = true;
    readBody = true;
    SAXParser parser = parserRef.get();
    if (parser == null) {
        parser = parserFactory.newSAXParser();
        parserRef = new WeakReference<SAXParser>(parser);
    }
    parser.parse(is, this);
    return page;
}

From source file:com.bekwam.mavenpomupdater.Main.java

@Override
public void start(Stage primaryStage) throws Exception {

    ////from  ww  w . ja  v a 2s . co m
    // handle command line options
    //
    Application.Parameters params = getParameters();

    List<String> unnamedList = params.getUnnamed();

    Options options = new Options();
    options.addOption("help", false, "Print this message");
    options.addOption("hidpi", false, "Use high-DPI scaling");

    CommandLineParser p = new BasicParser();
    CommandLine cmd = p.parse(options, unnamedList.toArray(new String[0]));

    HelpFormatter formatter = new HelpFormatter();

    if (cmd.hasOption("help")) {
        if (log.isDebugEnabled()) {
            log.debug("[START] running as help command");
        }
        formatter.printHelp("Main", options);
        return;
    }

    AbstractModule module = null;
    if (runningAsJNLP()) {

        if (log.isInfoEnabled()) {
            log.info("using jnlp module and jnlp favorites store");
        }
        module = new MPUJNLPModule();
    } else {

        if (log.isInfoEnabled()) {
            log.info("using standalone module and in-memory favorites store");
        }
        module = new MPUStandaloneModule();
    }

    //
    // setup google guice
    //
    final Injector injector = Guice.createInjector(module);

    BuilderFactory builderFactory = new JavaFXBuilderFactory();

    Callback<Class<?>, Object> guiceControllerFactory = clazz -> injector.getInstance(clazz);

    //
    // setup icons
    //
    primaryStage.getIcons().add(new Image("images/mpu_icon_64.png"));

    //
    // load fxml and wire controllers
    //
    FXMLLoader mainViewLoader = new FXMLLoader(getClass().getResource("mavenpomupdater.fxml"), null,
            builderFactory, guiceControllerFactory);
    Parent mainView = mainViewLoader.load();
    MainViewController mainViewController = mainViewLoader.getController();

    FXMLLoader alertViewLoader = new FXMLLoader(getClass().getResource("alert.fxml"), null, builderFactory,
            guiceControllerFactory);
    Parent alertView = alertViewLoader.load();

    //
    // i'm continuing this manual wiring to 1) accommodate a potential
    // bi-directional reference problem and 2) to make sure that guice
    // doesn't return different object for the main -> alert and alert ->
    // main dependency injections
    //

    final AlertController alertController = alertViewLoader.getController();
    mainViewController.alertController = alertController;
    alertController.mainViewControllerRef = new WeakReference<MainViewController>(mainViewController);

    //
    // add FlowPane, StackPane objects (defined in program and outside of 
    // FXML)
    //
    final FlowPane fp = new FlowPane();
    fp.setAlignment(Pos.CENTER);
    fp.getChildren().add(alertView);
    fp.getStyleClass().add("alert-background-pane");

    final StackPane sp = new StackPane();
    sp.getChildren().add(fp); // initially hide the alert
    sp.getChildren().add(mainView);

    //
    // setup scene
    //
    Scene scene = new Scene(sp);
    scene.getStylesheets().add("com/bekwam/mavenpomupdater/mpu.css");

    scene.setOnKeyPressed(keyEvent -> {
        KeyCode key = keyEvent.getCode();
        if (key == KeyCode.ESCAPE && (sp.getChildren().get(1) == fp)) {
            if (log.isDebugEnabled()) {
                log.debug("[ESCAPE]");
            }
            alertController.action();
        }
    });

    //
    // setup stage
    //
    primaryStage.setTitle("Maven POM Version Updater");
    primaryStage.setScene(scene);

    if (cmd.hasOption("hidpi")) {

        if (log.isInfoEnabled()) {
            log.info("running in Hi-DPI display mode");
        }
        primaryStage.setWidth(2560.0);
        primaryStage.setHeight(1440.0);
        primaryStage.setMinWidth(1920.0);
        primaryStage.setMinHeight(1080.0);

        mainViewController.adjustForHiDPI();

    } else {
        if (log.isInfoEnabled()) {
            log.info("running in normal display mode");
        }

        primaryStage.setWidth(1280.0);
        primaryStage.setHeight(800.0);
        primaryStage.setMinWidth(1024.0);
        primaryStage.setMinHeight(768.0);

    }

    primaryStage.show();
}