001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.examples.group.bgd; 020 021import org.apache.hadoop.io.LongWritable; 022import org.apache.hadoop.io.Text; 023import org.apache.reef.examples.group.bgd.data.Example; 024import org.apache.reef.examples.group.bgd.data.parser.Parser; 025import org.apache.reef.io.data.loading.api.DataSet; 026import org.apache.reef.io.network.util.Pair; 027 028import javax.inject.Inject; 029import java.util.ArrayList; 030import java.util.List; 031import java.util.logging.Level; 032import java.util.logging.Logger; 033 034/** 035 * The list of BGD examples. 036 */ 037public class ExampleList { 038 039 private static final Logger LOG = Logger.getLogger(ExampleList.class.getName()); 040 041 private final List<Example> examples = new ArrayList<>(); 042 private final DataSet<LongWritable, Text> dataSet; 043 private final Parser<String> parser; 044 045 @Inject 046 public ExampleList(final DataSet<LongWritable, Text> dataSet, final Parser<String> parser) { 047 this.dataSet = dataSet; 048 this.parser = parser; 049 } 050 051 /** 052 * @return the examples 053 */ 054 public List<Example> getExamples() { 055 if (examples.isEmpty()) { 056 loadData(); 057 } 058 return examples; 059 } 060 061 private void loadData() { 062 LOG.info("Loading data"); 063 int i = 0; 064 for (final Pair<LongWritable, Text> examplePair : dataSet) { 065 final Example example = parser.parse(examplePair.getSecond().toString()); 066 examples.add(example); 067 if (++i % 2000 == 0) { 068 LOG.log(Level.FINE, "Done parsing {0} lines", i); 069 } 070 } 071 } 072}